Sorting Parallel Arrays

旧城冷巷雨未停 提交于 2021-02-17 02:06:46

问题


Beginner in Java using an old textbook and Head First: Java books to figure some stuff out.

I have three arrays all parallel. I need to be able to sort by Title, Author, or Page Count based on user selection. I can sort one using Arrays.sort() but I'm getting hung up on how to sort the other two arrays to correspond to the new sorted one.

Say I sort the BookTitle array, but I'm going to need to display the proper author and page count of its respective arrays and I'm stumped.

do
{
    entry = JOptionPane.showInputDialog(null,
                                        "Enter your sort preference: \n" +
                                        "T = Sort by TITLE\n" +
                                        "A = Sort by AUTHOR\n" +
                                        "P = Sort by PAGE Count");

    c = entry.charAt(0);

    switch (c)
    {
    case 't':
    case 'T':
        Arrays.sort(BookTitle);
        for (int x = 0; x < BookTitle.length; x++)
        {
            msg += ("Title: " + BookTitle[x] + "\n");
        }
        JOptionPane.showMessageDialog(null, msg);
        isValid = true;
        break;

    case 'a':
    case 'A':
        isValid = true;
        break;

    case 'p':
    case 'P':
        isValid = true;
        break;

    default:
        JOptionPane.showMessageDialog(null, "Invalid entry");
        break;
    }
} while (isValid == false);

回答1:


Adapted from this post by @ScottStanchfield: sorting List<Class> by one of its variable

public class Book {
    String author;
    String title;
    String pages;
}

Collections.sort(list, new Comparator<Book>() {
    public int compare(Book c1, Book c2) {
        if (c1.pages > c2.pages)
            return -1;
        if (c1.pages < c2.pages)
            return 1;
        return 0;
    }
});

Book[] books;
Collections.sort(books);

I don't know Java, so if you see any mistakes, please correct them!




回答2:


You can create one Class with all the three attributes Author, Title, Pages. And then you can create 3 comparators which will compare in terms of Author, Title and Pages independently. And then you can take user input T, A, P like what you are doing buy you have to sort the array with the corresponding comparators. Pseudo code would be like:

Case A :
    Arrays.sort(ComparatorA);

case T:
    Arrays.sort(ComparatorT);

case P:
    Arrays.sort(ComparatorP);



回答3:


public class SortUtils {
    public static void sort(long[] x, long[] y) {
        for (int i = 0; i < x.length; i++) {
            for (int j = i; j > 0 && x[j - 1] > x[j]; j--) {
                swap(x, j, j - 1);
                swap(y, j, j - 1);
            }
        }
    }

    private static void swap(long anArray[], int a, int b) {
        long t = anArray[a];
        anArray[a] = anArray[b];
        anArray[b] = t;
    }
}



回答4:


First option is as above, chuck them into one class and make it comparable like Why should a Java class implement comparable?.

This only allows you to compare on one value, I think there is a way to choose what to sort on, but not that I know.

The other way... is to implement your own sort method. It can be a bit of work, but is probably good to do at least once so you can learn it. Insertion and selection sort are pretty easy to master, and the will allows you to move elements in each array in parallel.

UPDATE: As above you can have multiple compartors, check out this website as it seems to have everything you need.



来源:https://stackoverflow.com/questions/8089443/sorting-parallel-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!