简体   繁体   中英

Sorting Parallel Arrays

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);

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!

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);
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;
    }
}

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.

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