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,

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.

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

Hi jwenting,

Collection can be sorted. So using

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

will work, just has others have said.

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.

...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..

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.

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 article has been dead for over six months. Start a new discussion instead.