Created
June 27, 2017 07:17
-
-
Save anil477/7dad4096a83d13be35ef231e2eb38552 to your computer and use it in GitHub Desktop.
Sort an array of integer where the integer are very large and they need to be stored at string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.Comparator; | |
class CompareData implements Comparator<String> { | |
@Override | |
public int compare(String date1, String date2) { | |
if(date1.length() > date2.length()) { | |
return 1; | |
} | |
else if (date1.length() < date2.length()) { | |
return -1; | |
} else if (date1.length() == date2.length()) { | |
return date1.compareTo(date2); | |
} | |
return 0; | |
} | |
} | |
class Test{ | |
public static void main(String[] args){ | |
List<String> list = new ArrayList<String>(); | |
list.add("1234567890123456789012345678901234567890"); | |
list.add("1234567890123456789012345678901234567899"); | |
list.add("1234567890123456789012345678901234567896"); | |
list.add("123456789"); | |
list.add("123456788"); | |
// System.out.println("Before sorting: " + list); | |
Collections.sort(list, new CompareData()); | |
System.out.println("After sorting: " + list); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment