Monday, August 24, 2015

Sorting list contents

List content is sorted using Collections.sort(listObject,comparatorObject);
For example if have list of Student object and you want to sort it, then you need to create a new object of type Comparator. The logic of comparision is written in

int compare(Object object1,Object object2)

While overriding compare method you need to return the following :
Positive integer : if object 1 > object 2
Negative integer : if object 2> object 1
Zero : if object 1 = object 2

This logic will allow your list to be sorted in ascending order.

No comments:

Post a Comment

ec2-user@ec2 Permission denied