简体   繁体   中英

Sorting a list of objects based on different data members in java


I have this class:

public class Friend {

private String name;
private String location;
private String temp;
private String humidity;

public String getTemp() {
    return temp;
}

public void setTemp(String temp) {
    this.temp = temp;
}

public String getHumidity() {
    return humidity;
}

public void setHumidity(String humidity) {
    this.humidity = humidity;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}


public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}
}

I want to sort a List based on name, location, temp and humidity based on user input.
The user specifies by which data member the sorting has to be done. 用户指定必须由哪个数据成员进行排序。
What is the easiest way to do this?
Thank you.

Because you want to sort them by four different standards, implementing Comparable does not make sense. In this case, you may find that creating different Comparators for each sort-by parameter. However, you could implement Comparable for the most logical sort-by field, such as name. Otherwise, comparators are the way to go.

public class FriendNameComparator extends Comparator<Friend> {

    // assuming both are non-null for code simplicity; you may wish to change that
    public int compare(Friend f1, Friend f2) {
        return f1.getName().compareTo(f2.getName());
    }
}

public class FriendLocationComparator extends Comparator<Friend> {

    // assuming both are non-null for code simplicity; you may wish to change that
    public int compare(Friend f1, Friend f2) {
        return f1.getLocation().compareTo(f2.getLocation());
    }
}

// and so forth

Then, you can use the sort function of the Collections utility class to sort by the given comparator.

Collections.sort(friendsList, new FriendNameComparator()); // sorts by name
Collections.sort(friendsList, new FriendLocationComparator()); // sorts by location
// etc

Java has a static function called Collections.sort(List, Comparator) which sorts a (generified) List of objects given a custom Comparator which, given two objects of the same type, determines which one is ordered before the other.

Your task is to write a function which creates a Comparator which orders the objects based on its arguments and the user specified sort order. For example:

public Comparator<Friend> getComparator(final String sortBy) {
  if ("name".equals(sortBy)) {
    return new Comparator<Friend>() {
      @Override int compare(Friend f1, Friend f2) 
        return f1.getName().compareTo(f2.getName());
      }
    };
  } else if ("location".equals(sortBy)) {
    return new Comparator<Friend>() {
      @Override int compare(Friend f1, Friend f2) 
        return f1.getLocation().compareTo(f2.getLocation());
      }
    };
  } else if ("temp".equals(sortBy)) {
    // ...
  } else {
    throw new IllegalArgumentException("invalid sort field '" + sortBy + "'");
  }
}
List list=new ArrayList();

Use If else if for each criteria:

if(location ){
        Collections.sort(list, new Comparator () {
             public int compare(YourObject o1, YourObject o2) {
                    return o1.getLocation().compareTo(o2.getLocation());
                }

        });

    }

    } else if(temp ){

    ........
    }
    .......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM