排序:从高到低排序:从低到高

时间:2020-04-27 07:28:12

标签: java arrays string methods bubble-sort

使用此方法怎么办当我从高到低或从低到高放置时,它会排列吗?

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

输出排序方式:高到低John“,19索非亚,13 Ben”,9尤金,8 J鲁斯,5 排序方式:从低到高Jairus,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;
        }

2 个答案:

答案 0 :(得分:2)

由于您尝试以不同的方式进行排序,因此需要使用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 " + 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 age 8
Ben age 9
John age 19
Jairus age 5
Sofia age 13

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

High to low: 
John age 19
Sofia age 13
Ben age 9
Eugine age 8
Jairus age 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 " + 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 age 8
Ben age 9
John age 19
Jairus age 5
Sofia age 13

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

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

答案 1 :(得分:1)

我真的不明白您希望代码如何工作,但是如果您在简单地对这些值进行排序时遇到问题,那么我想您已经涵盖了。而不是存储在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);
    }
  }
}