Created
December 15, 2015 20:54
-
-
Save AnnaBoro/6cf910f03cf407ffea1f to your computer and use it in GitHub Desktop.
ArrayList + Comparator
This file contains hidden or 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
package lesson7.CompList; | |
import java.util.Comparator; | |
public class CompList implements Comparator { | |
@Override | |
public int compare(Object o1, Object o2) { | |
String oStr1 = (String) o1; | |
String oStr2 = (String) o2; | |
if (oStr1.compareTo(oStr2) > 0) { | |
return -1; | |
} else if (oStr1.compareTo(oStr2) > 0) { | |
return 1; | |
} else return 0; | |
} | |
} |
This file contains hidden or 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
package lesson7.CompList; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
public class Launcher { | |
public static void main(String[] args) { | |
ArrayList<String> arr = new ArrayList<String>(); | |
CompList cl = new CompList(); | |
arr.add("kolya"); | |
arr.add("yulya"); | |
arr.add("valya"); | |
arr.add("zoya"); | |
arr.add("lilya"); | |
arr.add("petr"); | |
arr.add("kirill"); | |
arr.add("kotya"); | |
arr.add("alya"); | |
arr.add("sasha"); | |
System.out.println(Arrays.toString(arr.toArray())); | |
Collections.sort(arr, cl); | |
System.out.println(Arrays.toString(arr.toArray())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment