排序方式:从高到低排序方式:从低到高Comparator.comparing

问题描述 投票:0回答:2

使用此方法时,我会从高到低或从低到高排列吗?

String [][]Data={{"Eugine","8"},{"Ben","9"},{"John","19"},{"Jairus","5"},{"Sofia","13"}};

[输出排序:从高到低John“,19索非亚,13 Ben”,9尤金,8 air鲁,5排序方式:低至高air牛,5尤金,8本,9索非亚,13约翰,19


//This is just my noob code// //please help//
 String [][] Data={{"Eugine","8"},{"Ben","9"},{"John","19"},{"Jairus","5"},{"Sofia","13"}};
            Arrays.sort(people);
            for(Person p : people)
                System.out.println(p.name + ", " + p.age);
        }
    }

    class Person implements Comparable<Person>{
        public final String name;
        public final int age;[enter image description here][1]
        public Person(String name, int age){
            this.name = name;
            this.age = age;
        }
        @Override
        public int compareTo(Person person) {
            return age - person.age;
        }
java arrays string methods bubble-sort
2个回答
0
投票

我无法真正理解您希望代码如何工作,但是如果您在简单地对这些值进行排序时遇到问题,那么我想我已经涵盖了。而不是存储在String数组中,请使用TreeMaps。

我将数字存储为键,将它们各自的名称存储为值,并将其存储在TreeMap中,该树按排序顺序(从低到高)存储它们。您也可以使用Collections.reverseOrder()以相反的顺序显示它们。

import java.util.*;

class Main {
  public static void main(String[] args) {

    TreeMap<Integer, String> map = new TreeMap<>();

    map.put(8, "Eugene");
    map.put(9, "Ben");
    map.put(19, "John");
    map.put(5, "Jairus");
    map.put(13, "Sofia");

    System.out.println("From low to high");

    for(Integer i : map.keySet()){
      System.out.println(map.get(i) + ", " + i);
    }

    TreeMap<Integer, String> revMap = 
      new TreeMap<Integer, String>(Collections.reverseOrder());

    revMap.putAll(map);

    System.out.println("\nFrom high to low");
    for(Integer i : revMap.keySet()){
      System.out.println(revMap.get(i) + ", " + i);
    }
  }
}

0
投票

由于您试图以不同的方式进行排序,因此需要使用Comparator

您可以使用的某些方法如下:

通过创建单独的比较器

import java.util.Arrays;
import java.util.Comparator;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return name + ", " + age;
    }
}

class LowToHighComparator implements Comparator<Person> {

    @Override
    public int compare(Person p1, Person p2) {
        return Integer.compare(p1.getAge(), p2.getAge());
    }
}

class HighToLowComparator implements Comparator<Person> {

    @Override
    public int compare(Person p1, Person p2) {
        return Integer.compare(p2.getAge(), p1.getAge());
    }
}

public class Main {
    public static void main(String[] args) {
        Person[] people = { new Person("Eugine", 8), new Person("Ben", 9), new Person("John", 19),
                new Person("Jairus", 5), new Person("Sofia", 13) };

        Person[] lowToHigh = people.clone();
        Person[] highToLow = people.clone();

        Arrays.sort(lowToHigh, new LowToHighComparator());
        Arrays.sort(highToLow, new HighToLowComparator());

        System.out.println("Unsorted: ");
        Arrays.stream(people).forEach(System.out::println);
        System.out.println();

        System.out.println("Low to high: ");
        Arrays.stream(lowToHigh).forEach(System.out::println);
        System.out.println();

        System.out.println("High to low: ");
        Arrays.stream(highToLow).forEach(System.out::println);
    }
}

输出:

Unsorted: 
Eugine, 8
Ben, 9
John, 19
Jairus, 5
Sofia, 13

Low to high: 
Jairus, 5
Eugine, 8
Ben, 9
Sofia, 13
John, 19

High to low: 
John, 19
Sofia, 13
Ben, 9
Eugine, 8
Jairus, 5

通过使用Comparator.comparing

import java.util.Arrays;
import java.util.Comparator;

class Person {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return name + ", " + age;
    }
}

public class Main {
    public static void main(String[] args) {
        Person[] people = { new Person("Eugine", 8), new Person("Ben", 9), new Person("John", 19),
                new Person("Jairus", 5), new Person("Sofia", 13) };

        Person[] lowToHigh = people.clone();
        Person[] highToLow = people.clone();

        Arrays.sort(lowToHigh, Comparator.comparing(Person::getAge));
        Arrays.sort(highToLow, Comparator.comparing(Person::getAge).reversed());

        System.out.println("Unsorted: ");
        Arrays.stream(people).forEach(System.out::println);
        System.out.println();

        System.out.println("Low to high: ");
        Arrays.stream(lowToHigh).forEach(System.out::println);
        System.out.println();

        System.out.println("High to low: ");
        Arrays.stream(highToLow).forEach(System.out::println);
    }
}

输出:

Unsorted: 
Eugine, 8
Ben, 9
John, 19
Jairus, 5
Sofia, 13

Low to high: 
Jairus, 5
Eugine, 8
Ben, 9
Sofia, 13
John, 19

High to low: 
John, 19
Sofia, 13
Ben, 9
Eugine, 8
Jairus, 5
热门问题
推荐问题
最新问题