Jahir Fiquitiva
Jahir Fiquitiva

Reputation: 1509

Sorting a matrix or 2d array by the content of a column

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:

The initial matrix is:

  • 1 - Martha - 12321 - 18
  • 2 - Adam - 54345 - 22
  • 3 - June - 76577 - 12
  • 4 - Thomas - 23454 - 16
  • 5 - Peter - 62356 - 24
  • 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.

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

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

    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
  • 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.

    Upvotes: 0

    Views: 646

    Answers (3)

    Goce Bogdanovski
    Goce Bogdanovski

    Reputation: 23

    In c# you can make model for that specific type of array and then you can use lambda to sort it.
    Exp. OrderBy(p=> p.Name)

    Upvotes: 0

    Ioannis Panteleakis
    Ioannis Panteleakis

    Reputation: 58

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

    Upvotes: 1

    Andreas
    Andreas

    Reputation: 159185

    Since a 2D array is actually an array of arrays, you sort the outer array using a custom Comparator<String[]> by calling Arrays.sort(array, comparator).

    Upvotes: 0

    Related Questions