选择同时对字符串和整数排序

时间:2018-10-23 10:37:39

标签: arrays file sorting selection-sort

这是我设置的任务:

创建一个名为Names_ages.txt的文本文件,其内容如下:

Jones 14
Abrams 15
Smith 19
Jones 9
Alexander 22
Smith 20
Smith 17
Tippurt 42
Jones 2
Herkman 12
Jones 11

每行是一个人的姓氏,后跟一个空格,然后是他的年龄。我们想按字母顺序对这些名称进行排序,如果名称重复,则按年龄升序排序。正确排序的列表将显示如下:

Abrams, 15
Alexander, 22
Herkman, 12
Jones, 2
Jones, 9
Jones, 11
Jones, 14
Smith, 17
Smith, 19
Smith, 20
Tippurt, 42

以下是我的(有效的)选择方法,分别用于字符串和整数:

private static void sort(String[] a) {

    String min;
    int minIndex;

    for (int i = 0; i < a.length; i++) {
        min = a[i];
        minIndex = i;
        // find minimum
        for (int j = i + 1; j < a.length; j++) {
            // salient feature
            if (a[j].charAt(0) < min.charAt(0)) {
                min = a[j];
                minIndex = j;
            }
        }
        a[minIndex] = a[i]; // swap
        a[i] = min;
    }

}

private static void sort(int[] a) {

    int min, minIndex;

    for (int i = 0; i < a.length; i++) {
        min = a[i];
        minIndex = i;
        // find minimum
        for (int j = i + 1; j < a.length; j++) {
            // salient feature
            if (a[j] < min) {
                min = a[j];
                minIndex = j;
            }
        }
        a[minIndex] = a[i]; // swap
        a[i] = min;
    }

} 

我可以对文本文件中的名称进行排序,然后对后面的数字进行排序,但是年龄最终与不正确的人相对应。这是我的类的主要方法:

Scanner scanner = new Scanner(new File("/Users/Krish/IdeaProjects/Lessons/src/Lesson40/MultipleKey/NamesAges.txt"));
    String text[] = new String[100];
    int index = 0;

    while (scanner.hasNext()) {
        text[index++] = scanner.nextLine();
    }

    scanner.close();

    String name;
    String[] names = new String[index];
    int age;
    int[] ages = new int[index];

    for (int i = 0; i < index; i++) {
        Scanner line = new Scanner(text[i]);
        name = line.next();
        names[i] = name;
        age = line.nextInt();
        ages[i] = age;
    }

    sort(names);
    sort(ages);

    for (int i = 0; i < index; i++) {
        System.out.println(names[i] + ", " + ages[i]);
    }

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

  1. 创建实现Person的POJO类Comparable<Person>
  2. 解析后,将Person个实例存储在集合中,例如List<Person>
  3. 对集合进行排序

-

public class PersonTest {
    static class Person implements Comparable<Person> {
        private static final Comparator<Person> COMPARATOR = Comparator
                .comparing(Person::getName)
                .thenComparingInt(Person::getAge);

        final String name;
        final int age;

        public static Person parse(String rec) {
            final String[] parts = rec.split(" ");
            return new Person(parts[0], Integer.valueOf(parts[1]));
        }

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

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        @Override
        public int compareTo(Person o) {
            return COMPARATOR.compare(this, o);
        }
    }

    @Test
    public void testSorting() throws Exception {
        final Person[] sortedPersons = Files.lines(Paths.get("/path/to/file.txt"))
                .map(Person::parse)
                .sorted() // sort it here
                .toArray(Person[]::new);

        // or instead, sort it here with your custom algorithm
        // using Person.COMPARATOR for comparison
    }
}

答案 1 :(得分:0)

public static void main(String[] args) throws IOException {

    Scanner scanner = new Scanner(new File("/Users/Krish/IdeaProjects/Lessons/src/Lesson40/MultipleKey/NamesAges"));
    String[] text = new String[100];
    int index = -1;

    while (scanner.hasNext()) {
        text[++index] = scanner.nextLine();
    }

    scanner.close();

    Scanner line;
    String[] name = new String[100];
    int[] age = new int[100];

    for (int i = 0; i <= index; i++) {
        line = new Scanner(text[i]);
        name[i] = line.next();
        age[i] = line.nextInt();
    }

    String minName;
    int minAge;
    int minIndex;

    for (int i = 0; i <= index; i++) {
        minName = name[i];
        minAge = age[i];
        minIndex = i;
        for (int j = i + 1; j <= index; j++) {
            if (name[j].compareTo(minName) == 0) {
                if (age[j] < minAge) {
                    minName = name[j];
                    minAge = age[j];
                    minIndex = j;
                }
            } else if (name[j].compareTo(minName) < 0) {
                minName = name[j];
                minAge = age[j];
                minIndex = j;
            }
        }
        name[minIndex] = name[i];
        name[i] = minName;
        age[minIndex] = age[i];
        age[i] = minAge;
    }

    for (int j = 0; j <= index; j++) {
        System.out.println(name[j] + ", " + age[j]);
    }

}