Skip to content

Instantly share code, notes, and snippets.

@Abhishek-Chaudhary-InfoCepts
Last active December 6, 2018 21:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Abhishek-Chaudhary-InfoCepts/cb5f12d5e97658dd9b177a24d30cef99 to your computer and use it in GitHub Desktop.
Save Abhishek-Chaudhary-InfoCepts/cb5f12d5e97658dd9b177a24d30cef99 to your computer and use it in GitHub Desktop.
package com.ge.current.points;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeMap;
import java.util.TreeSet;
public class SortData {
public static void main(String[] args) {
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
};
// Create a list of integers
ArrayList<Integer> coll_int = new ArrayList<Integer>();
coll_int.add(5);
coll_int.add(3);
coll_int.add(1);
coll_int.add(6);
coll_int.add(7);
// Ascending
Collections.sort(coll_int, (i1, i2) -> (i1 - i2));
System.out.println("ASC: " + coll_int);
// Descending
Collections.sort(coll_int, (i1, i2) -> (i2 - i1));
System.out.println("DESC: "+ coll_int);
// Create a list of strings
ArrayList<String> coll_string = new ArrayList<String>();
coll_string.add("Sample");
coll_string.add("Friends");
coll_string.add("Dear");
coll_string.add("Is");
coll_string.add("Superb");
// Ascending
Collections.sort(coll_string, (s1, s2) -> s1.compareTo(s2));
System.out.println("ASC: " + coll_string);
// Descending
Collections.sort(coll_string, (s1, s2) -> s2.compareTo(s1));
System.out.println("DESC: "+ coll_string);
TreeSet<Integer> treeset = new TreeSet<>(comparator);
TreeMap<Integer, String> treemap = new TreeMap<>(comparator);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment