java - Sorting 2d string array with integer -
i created 2d string array (called data) contains on first position integer (as string) , on second position link file.
examples:
["3", "test3.pdf"]; ["1", "test1.pdf"]; ["2", "test2.pdf"]; ["10", "test10.pdf"]
so need sort array ascending integer. result of sorting should sorted array like:
["1", "test1.pdf"]; ["2", "test2.pdf"];["3", "test3.pdf"];["10", "test10.pdf"]
i found sample code this:
arrays.sort(data, new comparator<string[]>() { @override public int compare(final string[] entry1, final string[] entry2) { final string time1 = entry1[0]; final string time2 = entry2[0]; return time1.compareto(time2); } });
but problem is, in case compares string logic, result be-> 1,10,2,3. cannot archive result this. know can do? can have 2d array of 1 type? not mix of string , integer?
the comparison can done converting first element integer, demonstrated in following snippet:
arrays.sort(data, new comparator<string[]>() { @override public int compare(final string[] entry1, final string[] entry2) { final integer time1 = integer.valueof(entry1[0]); final integer time2 = integer.valueof(entry2[0]); return time1.compareto(time2); } });
Comments
Post a Comment