java - Sorting ArrayList with natural sort order in Android? -


private arraylist<string> marraylist = new arraylist<string>();      marraylist.clear();      marraylist.add("test 3");     marraylist.add("test 21");     marraylist.add("test 4");     marraylist.add("test 6");     marraylist.add("test 1");      collections.sort(marraylist);      (int = 0; < marraylist.size(); i++) {          log.e("tag", "" + marraylist.get(i));     } 

result is:

test 1 test 21 test 3 test 4 test 6 

but want:

test 1 test 3 test 4 test 6 test 21 

i sorted not perfectly, how that, don't know, if have idea, please share me.

although not solution idea, try this:

collections.sort(marraylist, new comparator<string>() {             public int compare(string s1, string s2) {                integer i1 = integer.parseint(s1.split("test ")[1]);                integer i2 = integer.parseint(s2.split("test ")[1]);                 return i1.compareto(i2);             }); 

basically can sort object. recommend having data class containing variables can sort these objects on variable's basis. e.g.

public class data{     public string test;     public integer i;  }  arraylist<data> marraylist = new arraylist<data>(); . . .   collections.sort(marraylist, new comparator<data>() {             public int compare(data d1, data d2) {                  return d1.i.compareto(d2.i);             }); 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -