简体   繁体   中英

Sorting an array list with a delimiter

I have been stuck on this problem for so long and i have no idea what to do. Basically i have a text file with people names then student number then prize money like this:

Green%3243%1000
Kevin%7657%400
Frank%345%10000
Bob%5435%5000
Stefan%31231%1000
Javis%4532%100
IronMan%5435%2000
Lamp%534%3000

What i want to be able to do is sort the array based on the last number.

I tried this abomination (Don't bother reading it its garbage):

    boolean flag = true;
    String temp;
    int temp1;
    int temp2;

    while (flag){
        flag = false;
        for(int j=0;  j < list.size() -1;  j++ ){
            System.out.println(list.get(j));
            Scanner s = new Scanner(list.get(j)).useDelimiter("%");
            s.next();
            s.next();
            temp1 = s.nextInt();
            Scanner s2 = new Scanner(list.get(j+1)).useDelimiter("%");
            s2.next();
            s2.next();
            temp2 = s2.nextInt();
            if (temp1 < temp2){
                temp = list.get(j);
                list.add(j, list.get(j+1));
                list.add(j+1,temp);
                flag = true;
            } 
        } 
    }

But its just infinitely looping. My though while making it was just patching array lists into a bubble sort.

If anyone has any ideas and is willing to share them it will be greatly appreciated.

Here's something for you to get head started.

  1. Create a map for prize money => line as key/value pair
  2. Read each line in the file, parse it and put key/value pair in the above map
  3. Once your map is ready, convert the keys entry set into the collections like list
  4. Sort the collections, using Collections.sort()
  5. Iterate over the created map, and for each value in the collection get the corresponding value from the map.

Hope this helps you to get the workflow.

Java is an object-oriented language, so I'll just use objects:

  • Create a Student object with the three values you want to store (and a toString() method to print them separated by "%":

     public class Student { private final String name; private final int number; private final int prizeMoney; public Student(final String name, final int number, final int prizeMoney) { this.name = name; this.number = number; this.prizeMoney = prizeMoney; } @Override public String toString() { return name+"%"+number+"%"+prizeMoney; } public int getPrizeMoney() { return prizeMoney; } } 
  • Read your lines as Student objects, and store them in a List :

     final Scanner scan = new Scanner(new File("/path/to/StudentsList")); final List<Student> students = new ArrayList<Student>(); while (scan.hasNextLine()) { final Scanner line = new Scanner(scan.nextLine()); line.useDelimiter("%"); students.add(new Student(line.next(), line.nextInt(), line.nextInt())); line.close(); } scan.close(); 
  • Order the List with a custom Comparator , and print it:

     students.sort(new Comparator<Student>() { @Override public int compare(final Student s1, final Student s2) { return s1.getPrizeMoney()-s2.getPrizeMoney(); } }); for (final Student student: students) System.out.println(student); 

Output :

Javis%4532%100
Kevin%7657%400
Green%3243%1000
Stefan%31231%1000
IronMan%5435%2000
Lamp%534%3000
Bob%5435%5000
Frank%345%10000

我认为在这里创建一个3d数组,数组中从右到左的8x8x8是row,col,并且因此[0] [0] [1]是块1或kevin [0] [1] [1]是7657 [1 ] [1] [1]是400.我喜欢这种方式,因为它不仅为每个“项目”提供了一个数组,它还可以使它保持井然有序且易于访问

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