Skip to content

Instantly share code, notes, and snippets.

@Cartman0 Cartman0/SortSample.java
Last active Feb 28, 2016

Embed
What would you like to do?
Java のsort とComparator のサンプル
package sortsample;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SortSample {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
/*Arrays.sort 昇順
http://docs.oracle.com/javase/jp/8/docs/api/java/util/Arrays.html#sort-int:A-
*/
{
int array[] = {3, 2, 1, 5, 4};
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}
/*Arrays.sort Comparator
http://docs.oracle.com/javase/jp/8/docs/api/java/util/Arrays.html#sort-T:A-java.util.Comparator-
*/
{
System.out.println("Arrays.sort(,Comparator)");
Integer array[] = {3, 2, 1, 5, 4};
Arrays.sort(array, Comparator.naturalOrder());
System.out.println(Arrays.toString(array));
Arrays.sort(array, Comparator.reverseOrder());
System.out.println(Arrays.toString(array));
}
/*Collections.sort
http://docs.oracle.com/javase/jp/8/docs/api/java/util/Collections.html#sort-java.util.List-
*/
{
System.out.println("Collections sort:");
List<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 3, 2, 1, 4, 5);
Collections.sort(list);
System.out.println(list.toString());
}
/*
Collections.sort(,Comparator)
*/
{
System.out.println("Collections.sort(,Comparator)");
List<Integer> list = new ArrayList<Integer>();
Collections.addAll(list, 3, 2, 1, 4, 5);
list.sort(Comparator.naturalOrder());
System.out.println(list.toString());
Collections.sort(list, Comparator.reverseOrder());
System.out.println(list.toString());
}
/*
stream.sorted
http://docs.oracle.com/javase/jp/8/docs/api/java/util/stream/Stream.html#sorted--
*/
{
System.out.println("stream sorted");
int array[] = {3, 2, 1, 5, 4};
System.out.println(Arrays.toString(Arrays.stream(array).sorted().toArray()));
}
/*
stream.sorted(Comparator)
http://docs.oracle.com/javase/jp/8/docs/api/java/util/stream/Stream.html#sorted-java.util.Comparator-
*/
{
System.out.println("stream.sorted(Comparator)");
Integer array[] = {3, 2, 1, 5, 4};
System.out.println(Arrays.toString(Arrays.stream(array).sorted(Comparator.naturalOrder()).toArray()));
System.out.println(Arrays.toString(Arrays.stream(array).sorted(Comparator.reverseOrder()).toArray()));
}
/*
独自オブジェクトの配列 sort
*/
{
System.out.println("独自オブジェクト");
Person people[] = {
new Person("Sato", 20),
new Person("Tanaka", 15),
new Person("Suzuki", 35),
new Person("Yamamoto", 40),
new Person("Koyama", 10)
};
Arrays.sort(people, Comparator.comparing(Person::getAge));
System.out.println(Arrays.toString(people));
Arrays.sort(people, Comparator.comparing(Person::getAge).reversed());
System.out.println(Arrays.toString(people));
}
/*
独自オブジェクトの配列 stream.sorted
*/
{
System.out.println("独自オブジェクト stream.sorted");
Person people[] = {
new Person("Sato", 20),
new Person("Tanaka", 15),
new Person("Suzuki", 35),
new Person("Yamamoto", 40),
new Person("Koyama", 10)
};
System.out.println("age naturalorder:");
System.out.println(
Arrays.toString(Arrays.stream(people).sorted(Comparator.comparing(Person::getAge)).toArray())
);
System.out.println("age reverseorder:");
System.out.println(
Arrays.toString(Arrays.stream(people).sorted(Comparator.comparing(Person::getAge).reversed()).toArray())
);
System.out.println("name naturalorder:");
System.out.println(
Arrays.toString(Arrays.stream(people).sorted(Comparator.comparing(Person::getName)).toArray())
);
System.out.println("name reverseorder:");
System.out.println(
Arrays.toString(Arrays.stream(people).sorted(Comparator.comparing(Person::getName).reversed()).toArray())
);
}
}
}
class Person {
private String name_;
private int age_;
public Person(String name, int age) {
this.name_ = name;
this.age_ = age;
}
public String getName() {
return this.name_;
}
public int getAge() {
return this.age_;
}
@Override
public String toString() {
return this.name_ + ", " + this.age_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.