all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
47
Task/Sort-stability/Java/sort-stability.java
Normal file
47
Task/Sort-stability/Java/sort-stability.java
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
public class RJSortStability {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String[] cityList = { "UK London", "US New York", "US Birmingham", "UK Birmingham", };
|
||||
|
||||
String[] cn = cityList.clone();
|
||||
System.out.println("\nBefore sort:");
|
||||
for (String city : cn) {
|
||||
System.out.println(city);
|
||||
}
|
||||
|
||||
// sort by city
|
||||
Arrays.sort(cn, new Comparator<String>() {
|
||||
public int compare(String lft, String rgt) {
|
||||
return lft.substring(4).compareTo(rgt.substring(4));
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("\nAfter sort on city:");
|
||||
for (String city : cn) {
|
||||
System.out.println(city);
|
||||
}
|
||||
|
||||
cn = cityList.clone();
|
||||
System.out.println("\nBefore sort:");
|
||||
for (String city : cn) {
|
||||
System.out.println(city);
|
||||
}
|
||||
|
||||
// sort by country
|
||||
Arrays.sort(cn, new Comparator<String>() {
|
||||
public int compare(String lft, String rgt) {
|
||||
return lft.substring(0, 2).compareTo(rgt.substring(0, 2));
|
||||
}
|
||||
});
|
||||
|
||||
System.out.println("\nAfter sort on country:");
|
||||
for (String city : cn) {
|
||||
System.out.println(city);
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue