How to sort an list first letter ascending and last letter decending in java? -
i have list values
- 1alfreda
- 1alfredb
- 1alfredc
- 2benb
- 2benc
- 2benb
i want list sorted out first letter ascending , last letter descending sorted list
- 1alfredc
- 1alfredb
- 1alfreda
- 2benc
- 2benb
- 2bena
how can in java ? plz help.
not tested it, this, should work.
collections.sort(list, new comparator<string>() { public int compare(string e1, string e2) { int c = new character(e1.chartat(0)).compareto( e2.charat(0) ); if( c!=0 ) return c; return new character(e2.chartat(e2.length()-1)).compareto( e1.charat(e1.length()-1) ) } });
Comments
Post a Comment