我有2个班,一个是Grades,它是一个容器类。另一个是Grade,它是一个对象类。我用等级和名字对它们进行分类,等级从最高到最低,现在我尝试按名称对它进行排序,如果等级相同。我使用交换方法,但我无法交换容器类中的值。我只允许在驱动器中执行此操作,在两个类中都不进行编辑。请教我。
我的代码:
for(int i = 0 ; i < (sortedGrades.size()*sortedGrades.size()) ; i++){
for(int j = 0 ; j < sortedGrades.size() ; j++){
Grade a = sortedGrades.get(i);
Grade b = sortedGrades.get(i+1);
if(a.isSameGPA(b))
{
if(repeat.contains(a) == true &&
repeat.contains(b) == false)
{
Grade temp = a;
a = b;
b = temp;
sortedGrades.get(i);
}
}
}
}
结果应如下所示:
S005: Stacy, Lu 4.0 S004: Aseef, Hernandez 3.9 S006: Aseef, Nilkund 3.9 S002: Jim, NLN 3.9 S003: Misty, Fang 3.9 S009: Steve, Calderon 3.9 S016: Aseef, Simmons 3.9 S010: Raj, Singh 3.8 S018: Hamza, Nilkund 3.5 S012: Kathy, Calderon 3.5 S017: Hifza, Nilkund 3.3 S011: Jason, Kramer 3.3 S001: John, Rodgers 3.3 S019: Chris, Peach 3.2 S013: Roopa, Singh 3.2 S020: Ramona, Luke 2.4 S014: Amid, Naveed 2.4 S015: Faith, Williams 1.0
答案 0 :(得分:1)
如果您只想按字符串值双重值THEN排序列表,您可以这样做:
(用你的代码改变我的代码中的对象:Person = Grade,persons = sortedGrades)
Java 8 Style:
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person(4.0, "Name-3"));
persons.add(new Person(3.9, "Name-1"));
persons.add(new Person(2.0, "Name-6"));
persons.add(new Person(2.0, "Name-4"));
persons.add(new Person(1.8, "Name-5"));
persons.add(new Person(1.3, "Name-7"));
persons.add(new Person(1.3, "Name-2"));
persons.add(new Person(1.0, "Name-8"));
for( Person p: persons ) {
System.out.println(p.getName()+", " + p.getValue());
}
System.out.println("-------------------------");
Comparator<Person> comparator = Comparator.comparing(Person::getValue).reversed().thenComparing(Person::getName);
persons.sort(comparator);
for( Person p: persons ) {
System.out.println(p.getName()+", " + p.getValue());
}
}
要运行此Test-Method,您需要以下Person对象类:
public class Person {
private double value;
private String name;
public Person(double value, String name) {
super();
this.value = value;
this.name = name;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
或者您可以使用Collections.sort
方法和定制的比较器这样做:
public static void main(String[] args) {
List<Person> persons = new ArrayList<>();
persons.add(new Person(4.0, "Name-3"));
persons.add(new Person(3.9, "Name-1"));
persons.add(new Person(2.0, "Name-6"));
persons.add(new Person(2.0, "Name-4"));
persons.add(new Person(1.8, "Name-5"));
persons.add(new Person(1.3, "Name-7"));
persons.add(new Person(1.3, "Name-2"));
persons.add(new Person(1.0, "Name-8"));
for( Person p: persons ) {
System.out.println(p.getName()+", " + p.getValue());
}
System.out.println("-------------------------");
Collections.sort(persons, new Comparator<Person>() {
public int compare(Person p1, Person p2) {
Double v1 = p1.getValue();
Double v2 = p2.getValue();
int vComp = v2.compareTo(v1);
if (vComp != 0) {
return vComp;
}
String n1 = p1.getName();
String n2 = p2.getName();
return n1.compareTo(n2);
}});
for( Person p: persons ) {
System.out.println(p.getName()+", " + p.getValue());
}
}