按键和值排序

时间:2014-10-25 04:02:51

标签: java sorting quicksort key-value

我实现了快速排序,我希望按键排序,然后按值排序。我有重复的键和值,所以我需要它们主要按键排序,次要按值排序。例如,以下内容正确排序:

cat 533
dog 251
dog 533
dog 1021
ferret 31
ferret 477
zebra 398

我正在通过命令行指定的文件中读取数据。我想过使用哈希表,但我读到的是无法对哈希表进行排序。所以我的问题是我如何通过密钥进行排序,然后通过我的快速排序实现按值排序?现在我只是从文件中读取文本而不是整数。到目前为止,这是我的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

public class Quicksort
{
    //  static Hashtable<String, Integer> hash;
    static ArrayList<String> records;

    public static void main(String[] args) throws IOException
    {
        //  hash = new Hashtable<String, Integer>();
        records = new ArrayList<String>();
        if (0 < args.length)
        {
            readFile(args[0]);
        }
        else
        {
            System.err.println("Invalid arguments count:" + args.length);
            System.exit(0);
        }
        ArrayList<String> s = new ArrayList<String>();

        quickSort(records, 0, records.size() - 1);
        for (String k : records)
            System.out.println(k);

    }

    public static void readFile(String inFile) throws IOException
    {
        BufferedReader br = new BufferedReader(new FileReader(inFile));
        try
        {
            String line = br.readLine();

            while (line != null)
            {
                //System.out.println(line);
                String[] split = line.split("\\s+");
                records.add(split[0]);
                //  hash.put(split[0], Integer.parseInt(split[1]));

                line = br.readLine();
            }
        }
        finally
        {

            br.close();
        }
    }

    public static <T> void display(T[] a)
    {
        for (T b : a)
            System.out.println(b);
    }

    public static <T extends Comparable> void quickSort(ArrayList<T> a, int wall, int pivotIndex)
    {
        if (wall < pivotIndex)
        {
            int index = partition(a, wall, pivotIndex);
            quickSort(a, wall, index - 1);
            quickSort(a, index + 1, pivotIndex);
        }
    }

    public static <T extends Comparable<T>> int partition(ArrayList<T> a, int wall, int pivotIndex)
    {
        T currentPivot = a.get(pivotIndex);
        int leftOfWall = wall - 1;

        for (int j = wall; j <= pivotIndex - 1; j++)
        {
            if (a.get(j).compareTo(currentPivot) <= 0)
                exchange(a, ++leftOfWall, j);

        }

        exchange(a, leftOfWall + 1, pivotIndex);
        return leftOfWall + 1;
    }

    public static <T> void exchange(ArrayList<T> a, int b, int c)
    {
        T swap = a.get(b);
        a.set(b, a.get(c));
        a.set(c, swap);

    }
}

2 个答案:

答案 0 :(得分:1)

声明一个实现Comparable的记录类:

class Record implements Comparable<Record>
{

String key;
int value;

int compareTo(Record o1, Record o2)
{
int val = o1.key.compareTo(o2.key);
if (val != 0)
return val;

return o1.value.compareTo(o2.value);

}

现在将记录读入此类的实例并运行排序

答案 1 :(得分:1)

其中一个选项是创建一个新类:

public class Animal implements Comparabe<Animal> {
    private String type;
    private int number;
    public Animal(String type, int number) {
        this.type = type;
        this.number = number;
    }

    public int compareTo(Animal other) {
       //sorting logic here, first sort strings if they equals sort by integers.
    }
}

然后只列出动物static ArrayList<Animal> records;