按列的内容对矩阵或2d数组进行排序

时间:2015-10-24 20:18:05

标签: java arrays string sorting matrix

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

初始矩阵是:

  • 1 - Martha - 12321 - 18
  • 2 - Adam - 54345 - 22
  • 3月 - 6月 - 76577 - 12
  • 4 - Thomas - 23454 - 16
  • 5 - Peter - 62356 - 24
  • 如您所见,第一列是位置,第二列是名称,第三列是虚构ID,第四列是人物的年龄。

    这是一个String数组,因此使用String.valueOf(number);

    转换数值数据

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

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

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

  • 1 - Martha - 12321 - 18
  • 4 - Thomas - 23454 - 16
  • 2 - Adam - 54345 - 22
  • 5 - Peter - 62356 - 24
  • 3月 - 6月 - 76577 - 12
  • 如您所见,移动了整行,而不仅仅是单列中的项目。

    是的,有人能帮帮我吗?提前谢谢。

    3 个答案:

    答案 0 :(得分:1)

    这是使用Comparator和Collections的快速解决方案。它应该是自我解释的一切。每个字段都是一个字符串,但您也可以轻松使用整数。希望你觉得它很有用。

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

    答案 1 :(得分:0)

    由于2D数组实际上是一个数组数组,因此您可以通过调用Comparator<String[]>使用自定义Arrays.sort(array, comparator)对外部数组进行排序。

    答案 2 :(得分:0)

    在c#中,您可以为该特定类型的数组制作模型,然后您可以使用lambda对其进行排序 进出口。 OrderBy(p =&gt; p.Name)