Created
April 22, 2014 21:20
-
-
Save sjchmiela/11194750 to your computer and use it in GitHub Desktop.
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
package PersonZC; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.List; | |
/** | |
* Created by sjchmiela on 22.04.2014. | |
*/ | |
public class Main | |
{ | |
public static void main(String[] args) | |
{ | |
List<Person> people = new ArrayList<Person>(); | |
people.add(new Person("Mark", "Green", 27, 190)); | |
people.add(new Person("Ann", "Brown", 22, 170)); | |
//Person p = Person.getComparator(); | |
Collections.sort(people, new Person.SurnameComparator()); | |
System.out.println(people); | |
} | |
} |
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
package PersonZC; | |
import java.util.Comparator; | |
/** | |
* Created by sjchmiela on 22.04.2014. | |
*/ | |
public class Person implements Comparable<Person> | |
{ | |
String name; | |
String surname; | |
int age; | |
int height; | |
public String getName() { return this.name; } | |
Person(String name, String surname, int age, int height) //konstruktor | |
{ | |
this.name = name; | |
this.surname = surname; | |
this.age = age; | |
this.height = height; | |
} | |
@Override | |
public int compareTo(Person p) | |
{ | |
int surnameDiff = surname.compareToIgnoreCase(p.surname); | |
int nameDiff = name.compareToIgnoreCase(p.name); | |
if(surnameDiff != 0) return surnameDiff; | |
if(nameDiff != 0) return nameDiff; | |
return age - p.age; | |
} | |
static class SurnameComparator implements Comparator<Person> | |
{ | |
@Override | |
public int compare(Person p1, Person p2) | |
{ | |
int comparedSurnames = p1.getName().compareTo(p2.surname); | |
if(comparedSurnames == 0) return p1.name.compareTo(p2.name); | |
else return comparedSurnames; | |
} | |
} | |
static class NameComparator implements Comparator<Person> | |
{ | |
@Override | |
public int compare(Person p1, Person p2) | |
{ | |
int comparedNames = p1.name.compareTo(p2.name); | |
if(comparedNames == 0) return p1.surname.compareTo(p2.surname); | |
else return comparedNames; | |
} | |
} | |
static class AgeComparator implements Comparator<Person> | |
{ | |
@Override | |
public int compare(Person p1, Person p2) | |
{ | |
return p1.age - p2.age; | |
} | |
} | |
static class HeightComparator implements Comparator<Person> | |
{ | |
@Override | |
public int compare(Person p1, Person p2) | |
{ | |
return p1.height - p2.height; | |
} | |
} | |
public static Comparator<Person> getComparator(int flag) | |
{ | |
switch(flag) | |
{ | |
case 1: | |
return new SurnameComparator(); | |
case 2: | |
return new NameComparator(); | |
case 3: | |
return new AgeComparator(); | |
case 4: | |
return new HeightComparator(); | |
default: | |
throw new IllegalArgumentException("Bad flag value: " + flag); | |
} | |
} | |
@Override | |
public String toString() | |
{ | |
return surname + " " + name + ", age: " + age + " height: " + height; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment