1

I have an array of objects and each objects has a given name and a surname. These names are written to the object using methods getGivenName and getSurname.

I need to sort the elements in the array in alphabetical order by surname. how can I do this?

user2914851
  • 57
  • 1
  • 1
  • 6

3 Answers3

7

Your comparator

class SampleComparator implements Comparator<YourObject> {
    @Override
    public int compare(YourObject o1, YourObject o2) {

           return o1.getSurname().compareTo(o2.getSurname());
   }
}

Your Sorting

  Collections.sort(YourList, new SampleComparator())

if you need ignore case then use like

  return o1.getSurname().compareToIgnoreCase(o2.getSurname()); 
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • I have 1000 objects in the array is there a way I can do it more efficiently? – user2914851 Oct 24 '13 at 08:53
  • 5
    what makes you think that this is not efficient? – ant Oct 24 '13 at 08:56
  • Note that String.compareTo will compare Strings based on the Unicode value of each character in the strings, which might not be what you want/ expect, depending on your input Strings and locale. See my answer. – Puce Oct 24 '13 at 11:35
0

Use List rather than using Array. Your class needs to implements Comparable interface. Please see the code,

By Implementing Comparable interface

public class Person implements Comparable<Person>{

private String givenName;
private String surname;

public static void main(String[] args) {
    Person person1 = new Person("a","b");
    Person person2 = new Person("c","d");
    Person person3 = new Person("e","f");

    List<Person> personList = new ArrayList<Person>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    Collections.sort(personList);
    System.out.println(personList);
}

@Override
public String toString() {
    return "WorkSheet [givenName=" + givenName + ", surname=" + surname
            + "]";
}
    public Person() {
        // TODO Auto-generated constructor stub
    }


public Person(String givenName , String surname) {
    this.givenName = givenName;
    this.surname = surname;
}

public String getGivenName() {
    return givenName;
}

public void setGivenName(String givenName) {
    this.givenName = givenName;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

@Override
public int compareTo(Person o) {
    // TODO Auto-generated method stub
    return o.getSurname().compareTo(this.getSurname());
}
}   

By Implementing Comaparator Interface

public class Person implements Comparator<Person>{

private String givenName;
private String surname;

public static void main(String[] args) {
    Person person1 = new Person("a","b");
    Person person2 = new Person("c","d");
    Person person3 = new Person("e","f");

    List<Person> personList = new ArrayList<Person>();
    personList.add(person1);
    personList.add(person2);
    personList.add(person3);
    Collections.sort(personList , new Person());
    System.out.println(personList);
}

@Override
public String toString() {
    return "WorkSheet [givenName=" + givenName + ", surname=" + surname
            + "]";
}

public Person() {
    // TODO Auto-generated constructor stub
}

public Person(String givenName , String surname) {
    this.givenName = givenName;
    this.surname = surname;
}

public String getGivenName() {
    return givenName;
}

public void setGivenName(String givenName) {
    this.givenName = givenName;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

@Override
public int compare(Person o1, Person o2) {
    // TODO Auto-generated method stub
    return o2.getSurname().compareTo(o1.getSurname());
}
}

Prateek
  • 12,014
  • 12
  • 60
  • 81
0

When sorting natural language texts, it's recommended to use a Collator and CollationKeys. The String.compareTo method mentioned by other answers will compare Strings based on the Unicode value of each character in the strings, which might not be what you want/ expect, depending on your input Strings and locale.

Note that I wrote some utility methods to help sorting natural language texts.

E.g. you coud try something like:

public class PersonLocalizer implements Localizer<Person> {
    @Override
    public String getDisplayString(Person person, Locale inLocale) {
        return person.getSurname();
    }
} 

[...]

Localizables.sort(new PersonLocalizer (), persons);

Maven:

<dependency>  
    <groupId>org.softsmithy.lib</groupId>  
    <artifactId>softsmithy-lib-core</artifactId>  
    <version>0.3</version>  
</dependency>  
Puce
  • 37,247
  • 13
  • 80
  • 152