Mark Yo
Mark Yo

Reputation: 49

java container object swap array method

I have 2 class, one is Grades, it's a container class. another one is Grade, it's a object class. I am sorting them with grades and names, grades has been done from highest to lowest, now I tried to sort it by names if the grades are the same. I use swap approach, but I am not able to swap the value in container class. And I only allow to do it within the drive, no editing in both class. Please lecture me.

My code:

      for(int i = 0 ; i < (sortedGrades.size()*sortedGrades.size()) ; i++){
            for(int j = 0 ; j < sortedGrades.size() ; j++){
                Grade a = sortedGrades.get(i);
                Grade b = sortedGrades.get(i+1);
                if(a.isSameGPA(b))
                {
                    if(repeat.contains(a) == true &&
                            repeat.contains(b) == false)
                    {
                       Grade temp = a;
                       a = b;
                       b = temp;

                       sortedGrades.get(i);
                    }
                }
            }
        }

outcome should look like this:

S005: Stacy, Lu 4.0
S004: Aseef, Hernandez 3.9
S006: Aseef, Nilkund 3.9
S002: Jim, NLN 3.9
S003: Misty, Fang 3.9
S009: Steve, Calderon 3.9
S016: Aseef, Simmons 3.9
S010: Raj, Singh 3.8
S018: Hamza, Nilkund 3.5
S012: Kathy, Calderon 3.5
S017: Hifza, Nilkund 3.3
S011: Jason, Kramer 3.3
S001: John, Rodgers 3.3
S019: Chris, Peach 3.2
S013: Roopa, Singh 3.2
S020: Ramona, Luke 2.4
S014: Amid, Naveed 2.4
S015: Faith, Williams 1.0

Upvotes: 0

Views: 153

Answers (1)

Rami.Q
Rami.Q

Reputation: 2476

if you just want to sort a list first by double Value THEN by String Value, you can do it this way:

(Change the Objects in my Code with yours: Person = Grade, persons = sortedGrades)

Java 8 Style:

public static void main(String[] args) {
        List<Person> persons = new ArrayList<>();
        persons.add(new Person(4.0, "Name-3"));
        persons.add(new Person(3.9, "Name-1"));
        persons.add(new Person(2.0, "Name-6"));
        persons.add(new Person(2.0, "Name-4"));
        persons.add(new Person(1.8, "Name-5"));
        persons.add(new Person(1.3, "Name-7"));
        persons.add(new Person(1.3, "Name-2"));
        persons.add(new Person(1.0, "Name-8"));
        for( Person p: persons ) {
            System.out.println(p.getName()+", " + p.getValue());
        }
        System.out.println("-------------------------");
        Comparator<Person> comparator = Comparator.comparing(Person::getValue).reversed().thenComparing(Person::getName);
        persons.sort(comparator);
        for( Person p: persons ) {
            System.out.println(p.getName()+", " + p.getValue());
        }
    }

to run this Test-Method, you need the following Person Object Class:

public class Person {
    private double value;
    private String name;
    public Person(double value, String name) {
        super();
        this.value = value;
        this.name = name;
    }
    public double getValue() {
        return value;
    }
    public void setValue(double value) {
        this.value = value;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

OR you can do it using the Collections.sort Method with your customized Comparator this way:

public static void main(String[] args) {
    List<Person> persons = new ArrayList<>();
    persons.add(new Person(4.0, "Name-3"));
    persons.add(new Person(3.9, "Name-1"));
    persons.add(new Person(2.0, "Name-6"));
    persons.add(new Person(2.0, "Name-4"));
    persons.add(new Person(1.8, "Name-5"));
    persons.add(new Person(1.3, "Name-7"));
    persons.add(new Person(1.3, "Name-2"));
    persons.add(new Person(1.0, "Name-8"));
    for( Person p: persons ) {
        System.out.println(p.getName()+", " + p.getValue());
    }
    System.out.println("-------------------------");

    Collections.sort(persons, new Comparator<Person>() {

        public int compare(Person p1, Person p2) {
            Double v1 = p1.getValue();
            Double v2 = p2.getValue();
            int vComp = v2.compareTo(v1);
            if (vComp != 0) {
                   return vComp;
            }               
            String n1 = p1.getName();
            String n2 = p2.getName();
            return n1.compareTo(n2);
    }});

    for( Person p: persons ) {
        System.out.println(p.getName()+", " + p.getValue());
    }
}

Upvotes: 1

Related Questions