0

Hello All,

I want to sort my list order by sno.

This is my code,

List<Employee>list=new ArrayList<Employee>();

Employee class has fields, one of field is sno (its String type); this field values are like this format

1.2.1
1.2.3
1.5.5
1.5.3

I want to sort this serial no. I used comparator but I dint get sorted output. Is there any option to sort this format of values?

Thanks,

5
Contributors
8
Replies
37
Views
2 Years
Discussion Span
Last Post by 2teez
0

A Comparator is the right way to do it, so there seems to be something wrong with your implementation. If you post your code someone will help.

0

ArrayList isn't sorted. You need to not just use a Comparator but also a sorted Collection, like TreeSet.

0

Hi jwenting,

Collection can be sorted. So using

Collections.sort(collectionName, new Comparator<collectionName>(){ ... };);

will work, just has others have said.

0

yes, you can use Collections.sort, but that doesn't get you a List that will retain sort order if you add items to it.

0

...but that doesn't get you a List that will retain sort order if you add items to it.

The OP didn't say anything about adding items after sorting have been done. Did s/he?

More so, the sorting routine could be made a method and then it's called whenever..

0

OP claimed to have used a Comparator and not get a sorted output. Most likely they applied the Comparator BEFORE adding the data to the Collection. Using a SortedSet implementation allows that.

0

OP claimed to have used a Comparator and not get a sorted output

And that is why am with [ JamesCherrill ] on this with the post A Comparator is the right way to do it, so there seems to be something wrong with your implementation. If you post your code someone will help.
That is about a week ago now.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.