由列的内容排序矩阵或二维数组

由列的内容排序矩阵或二维数组

问题描述:

我在寻找一些帮助code到一列的内容矩阵的内容进行分类,但保持在同一行的数据...我会尽量展示一个例子:

I'm looking for some help with the code to sort a matrix content by the content of one column, but keeping the data of the same row... I will try to show an example:

最初的矩阵是:

  • 1 - 玛莎 - 12321 - 18

  • 2 - 亚当 - 54345 - 22

  • 3日 - 6月 - 76577 - 12

  • 4 - 托马斯 - 23454 - 16

  • 5 - 彼得 - 62356 - 24
  • 正如你可以看到,第一列是位置,第二个是名字,第三是一个假想的ID,和第四个是人的年龄。

    As you can see, the first column is the position, second is the name, third is an imaginary ID, and fourth is the age of the person.

    这是一个String数组,所以数值数据使用将String.valueOf(数字)转换;

    This is a String array, so numerical data is converted using String.valueOf(number);

    现在,我想获得帮助与code到每一行进行排序,按任何列的内容。

    Now, I would like to get help with the code to sort each row, by the content of any column.

    例如,由名字列排序行,应该是这样的:

  • 2 - 亚当 - 54345 - 22

  • 3日 - 6月 - 76577 - 12

  • 1 - 玛莎 - 12321 - 18

  • 5 - 彼得 - 62356 - 24

  • 4 - 托马斯 - 23454 - 16
  • For example, rows sorted by the names column, should be something like:

  • 2 - Adam - 54345 - 22
  • 3 - June - 76577 - 12
  • 1 - Martha - 12321 - 18
  • 5 - Peter - 62356 - 24
  • 4 - Thomas - 23454 - 16
  • 现在,由ID列排序行,应该是这样的:

  • 1 - 玛莎 - 12321 - 18

  • 4 - 托马斯 - 23454 - 16

  • 2 - 亚当 - 54345 - 22

  • 5 - 彼得 - 62356 - 24

  • 3日 - 6月 - 76577 - 12
  • Now, rows sorted by the ID column, should be something like:

  • 1 - Martha - 12321 - 18
  • 4 - Thomas - 23454 - 16
  • 2 - Adam - 54345 - 22
  • 5 - Peter - 62356 - 24
  • 3 - June - 76577 - 12
  • 正如你所看到的,完整的行,很感动,而不是仅仅在单个列的项目。

    As you can see, the complete row was moved, instead of just the items in a single column.

    也许有人能帮助我吗?先谢谢了。

    May someone help me? Thanks in advance.

    下面是一个使用比较器和集合的快速解决方案。一切都应该是自我解释。每一个字段是一个字符串,但你可以很容易地使用整数为好。希望你觉得它有用。

    Here's a quick solution using Comparator and Collections. Everything it should be self explanatory. Every field is a String, but you could easily use integer as well. Hope you find it useful.

    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    
    public class MatrixCompare {
    
        public static void main(String[] args) {
    
            List<Contact> contacts = new ArrayList<Contact>();
            contacts.add(new Contact("1", "John", "456456", "35"));
            contacts.add(new Contact("2", "Jack", "123123", "20"));
            contacts.add(new Contact("3", "Mary", "234234", "24"));
            contacts.add(new Contact("4", "Jane", "345345", "18"));
    
            System.out.println("Initial List\n");
            printContacts(contacts);
    
            System.out.println("\n\nSorted by Id\n");
            Collections.sort(contacts, new SortById());
            printContacts(contacts);
    
            System.out.println("\n\nSorted by Name\n");
            Collections.sort(contacts, new SortByName());
            printContacts(contacts);
    
            System.out.println("\n\nSorted by Person Id\n");
            Collections.sort(contacts, new SortByPersonId());
            printContacts(contacts);
    
            System.out.println("\n\nSorted by Age\n");
            Collections.sort(contacts, new SortByAge());
            printContacts(contacts);
    
        }
    
        private static void printContacts(List<Contact> contacts) {
            for (int i=0; i<contacts.size(); i++) {
                System.out.println(contacts.get(i).id + " - " + contacts.get(i).name + " - " + 
                        contacts.get(i).getPersonId() + " - " + contacts.get(i).getAge());
            }
        }
    
        private static class Contact {
    
            String id;
            String name;
            String personId;
            String age;
    
            public Contact(String id, String name, String personId, String age) {
                this.id = id;
                this.name = name;
                this.personId = personId;
                this.age = age;
            }
    
            public String getId() {
                return id;
            }
    
            public String getName() {
                return name;
            }
    
            public String getPersonId() {
                return personId;
            }
    
            public String getAge() {
                return age;
            }
    
        }
    
        private static class SortById implements Comparator<Contact> {
            @Override
            public int compare(Contact o1, Contact o2) {
                String name1 = o1.getId();
                String name2 = o2.getId();  
                return name1.compareTo(name2);
            }
        }
        private static class SortByName implements Comparator<Contact> {
            @Override
            public int compare(Contact o1, Contact o2) {
                String name1 = o1.getName();
                String name2 = o2.getName();    
                return name1.compareTo(name2);
            }
        }
        private static class SortByPersonId implements Comparator<Contact> {
            @Override
            public int compare(Contact o1, Contact o2) {
                String name1 = o1.getPersonId();
                String name2 = o2.getPersonId();    
                return name1.compareTo(name2);
            }
        }
        private static class SortByAge implements Comparator<Contact> {
            @Override
            public int compare(Contact o1, Contact o2) {
                String name1 = o1.getAge();
                String name2 = o2.getAge(); 
                return name1.compareTo(name2);
            }
        }
    
    }