Skip to content
Advertisement

Sort function in Java collections

If I have following list of student objects,

JavaScript

I need to sort these objects in following order.

JavaScript

I have tried following code,

JavaScript

I am getting following result.

JavaScript

But I need all zero’s should be at last.

Advertisement

Answer

A better way to sort them with 0 higher than any value is:

JavaScript

This will correctly handle the case where obj.Rank=Integer.MAX_VALUE.

If your Ranks are Integers rather than ints you’ll also need to handle the case where they’re null.

Advertisement