java - 按列的内容对矩阵或二维数组进行排序

标签 java arrays string sorting matrix

我正在寻找一些有关代码的帮助,以按一列的内容对矩阵内容进行排序,但保留同一行的数据......我将尝试展示一个示例:

初始矩阵为:

  • 1 - 玛莎 - 12321 - 18
  • 2 - 亚当 - 54345 - 22
  • 3 - 6 月 - 76577 - 12
  • 4 - 托马斯 - 23454 - 16
  • 5 - 彼得 - 62356 - 24
  • 如您所见,第一列是职位,第二列是姓名,第三列是虚构的 ID,第四列是该人的年龄。

    这是一个字符串数组,因此数值数据使用 String.valueOf(number); 进行转换

    现在,我想获得有关按任意列的内容对每行进行排序的代码的帮助。

    例如,按名称列排序的行应该类似于:

  • 2 - 亚当 - 54345 - 22
  • 3 - 6 月 - 76577 - 12
  • 1 - 玛莎 - 12321 - 18
  • 5 - 彼得 - 62356 - 24
  • 4 - 托马斯 - 23454 - 16
  • 现在,按 ID 列排序的行应该类似于:

  • 1 - 玛莎 - 12321 - 18
  • 4 - 托马斯 - 23454 - 16
  • 2 - 亚当 - 54345 - 22
  • 5 - 彼得 - 62356 - 24
  • 3 - 6 月 - 76577 - 12
  • 如您所见,整行都被移动,而不仅仅是单列中的项目。

    有人可以帮助我吗?提前致谢。

    最佳答案

    这是使用比较器和集合的快速解决方案。一切都应该是不言自明的。每个字段都是一个字符串,但您也可以轻松使用整数。希望您觉得它有用。

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

    关于java - 按列的内容对矩阵或二维数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33322874/

    相关文章:

    java - 通过单击 ListView 项目来配对两个蓝牙设备,Android

    java - 编译过程中复制文件

    java - Java中将String格式的日志记录划分为列

    string - 如何在原始(多行)Kotlin 字符串中使用美元符号 '$'?

    java - 如何在java中检查1是质数并且Hashmap的输出对于String类型是错误的

    java - 我们如何在 Java 中定义函数 vector ,让函数记住它们的参数?

    c++ - 直接从指针/地址访问数组的元素

    javascript - 如何打印数组中列出的字典中的键和值,javascript

    python - 比较python中的希伯来语字符串

    c++ - 将十六进制写入文件