Java sorting an object array

java sort array of objects by property
java sort array of objects by field
array.sort java
java sort list of objects by field
java sort arraylist of objects
collections.sort java
how to sort objects in java
how to sort an array of objects alphabetically in java

I have a member application, the data is going into an object and then the object is being stored in the array. I am using string and integers in the object.

I am writing a method to display all male members and sort them by their score but I am struggling with the logic for this.

I am thinking I might need to create another array to store the male members in once I have got them out of the main array but am not really sure.

Could anyone help please.

public static void displayAllMaleMembers(){     
    System.out.println("List of male members:\n");

    for (int i=0; i < memberCount; i++) {                    
        Member member = memberList[i];                  
        if (member.getGender().equals("male") && member.getScore() > 50){
            System.out.println("Name:  " +member.getName());
            System.out.println("Score: " +member.getScore());
            System.out.println();   
        }           
        Arrays.sort(memberList, Collections.reverseOrder()); 
    }
    System.out.println("The sorted names by score are:");

    for (Member member : memberList) {
        System.out.println("Name:     " +member.getName());
        System.out.println("Handicap: " +member.getScore());
    }
}
The Problem
  1. You're sorting the entire memberList whereas you should first find all the students satisfying the said criteria and then sort the list once.
  2. You should not sort the memberList in each iteration as it's wrong and very inefficient.
  3. When it comes to printing, again you're printing the entire list not just the male members with scores greater than 50.
The Solution

First, you will need to copy only the male members with scores greater than 50 to a list:

List<Member> result = new ArrayList<>();
for(Member member : memberList)
    if ("male".equals(member.getGender()) 
                && member.getScore() > 50)
          result.add(member);

then sort it descending by member score:

result.sort(Comparator.comparingInt(Member::getScore).reversed());

then print:

result.forEach(member -> {
     System.out.println("Name:  " +member.getName());
     System.out.println("Score: " +member.getScore()); 
     System.out.println();   
});

Full code:

List<Member> result = new ArrayList<>();
for(Member member : memberList)
    if ("male".equals(member.getGender()) 
                && member.getScore() > 50)
      result.add(member);

result.sort(Comparator.comparingInt(Member::getScore).reversed());
result.forEach(member -> {
     System.out.println("Name:  " +member.getName());
     System.out.println("Score: " +member.getScore()); 
     System.out.println();   
});

Since you only want a result of male members sorted descending based on some criteria, you could also go with the stream approach simply because it's more readable and easier for this type of use case i.e. filtering and then applying further logic:

Arrays.stream(memberList) // create a Stream<Member>
      .filter(m -> "male".equals(m.getGender()) &&
            m.getScore() > 50) // retain only male members with score greater than 50
      .sorted(Comparator.comparingInt(Member::getScore).reversed()) // sort based on score descending i.e. highest to lowest
      .forEachOrdered(m -> {
             System.out.println("Name:     " +m.getName());
             System.out.println("Handicap: " +m.getScore());
             System.out.println();   
      }); // print the result
  1. Arrays.stream creates a Stream<Member>
  2. filter retains only the male members with score greater than 50
  3. sorted sorts the members based on their scores from highest to lowest.
  4. forEachOrdered then prints the results.

How to sort an array of objects in Java?, In order to sort an array of objects using Arrays.sort method in Java, we can have the class implement the Comparable interface which then imposes a natural  Use Arrays.sort () method to sort an array of objects. The objects must implement the Comparable interface which will decide their sorting order. All elements in the array must implement the Comparable interface. All elements in the array must be mutually comparable and must not throw a ClassCastException while comparing.

You need to have your Member class implement Comparable and then implement the compareTo function however you want

https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

Sort an array of objects in Java, Use Arrays. sort() method to sort an array of objects. The objects must implement the Comparable interface which will decide their sorting order. All elements in the array must implement the Comparable interface. The java.util.Arrays.sort (Object []) method sorts the specified array of Objects into ascending order according to the natural ordering of its elements.

If your Java version is 1.8 or greater , you can use Stream with an easiest way.

final List<Member> memberListWhichIsMaleAndScoreGreaterThan50 = Arrays.stream(memberList)
        .filter(member -> member.getGender().equals("male") && member.getScore() > 50)
        .collect(Collectors.toList());
//do  whatever you want on this list.

Here filter is filtering the list with gender and score. After that collecting to list to make an operation if you want.

Java Array Sort - Arrays.sort() Example, for objects that implement Comparable or use a Comparator . Sort ArrayList of Objects Using Comparable. Comparable is used to sort an arraylist of object on the basis of single property at a time. For example we have student data in which we have two properties, name and age. We can sort it by name or age. This can be done by implementing Comparable interface and overriding its compareTo() method. Student.java

I have a nice solution for you. Using constructors and getters and setters, without complex functions. Due to you only need to print name and score you can concatenate them as a single string, sort them and finally divide the string.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class foo {

public Member me = new Member();

public void displayAllMaleMembers() {

    System.out.println("List of male members:");
    for (Member member : me.getMembers()) {
        if (member.getGender().equals("male") && member.getScore() > 50) {
            System.out.println("Name:  " + member.getName());
            System.out.println("Score: " + member.getScore());
            System.out.println();
        }
    }

    System.out.println("The sorted names by score are:");
    String[] sort = new String[me.getMembers().size()];
    // concatenate name + comma + score
    for (int i = 0; i < sort.length; i++) {
        sort[i] = me.getMembers().get(i).getName() + "," + me.getMembers().get(i).getScore();
    }
    Arrays.sort(sort, Collections.reverseOrder()); // sort the concatenated string
    // prints
    for (String s : sort) {
// Split() divides the concatenated string, 
// [0] is the first position of the string[] returned by spit(",");
        System.out.println("Name:     " + s.split(",")[0]); // string before comma: name
        System.out.println("Handicap: " + s.split(",")[1]); // string after comma: score
    }
}

//Main
public static void main(String[] args) {

    foo f = new foo();
    ArrayList<Member> m = new ArrayList<Member>();
    // Creates an arraylist using the constructor field from Member class
    m.add(new Member("Criss", 80, "male"));
    m.add(new Member("Frank", 20, "male"));
    m.add(new Member("Ann", 80, "femmale"));

    // adds the arraylist through setMembers method
    f.me.setMembers(m);
    // print data
    f.displayAllMaleMembers();
}

}

Getters and setters class: The arraylist<Members> stores all attributtes you need (score, name, gender)

import java.util.ArrayList;
public class Member {

String Name;
int Score;
String Gender;
ArrayList<Member> members = new ArrayList<>();

//Constructor using fields
public Member(String name, int score, String gender) {
    super();
    Name = name;
    Score = score;
    Gender = gender;
} 

// default
Member(){
}

public ArrayList<Member> getMembers() {
    return members;
}

// Creates an arrayList of Members
public void setMembers(ArrayList<Member> members) {
    this.members.addAll(members); //adds all arraylist items
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

public int getScore() {
    return Score;
}

public void setScore(int score) {
    Score = score;
}

public String getGender() {
    return Gender;
}

public void setGender(String gender) {
    Gender = gender;
}
}

Output:

List of male members:
Name:  Criss
Score: 80

The sorted names by score are:
Name:     Frank
Handicap: 20
Name:     Criss
Handicap: 80
Name:     Ann
Handicap: 80

Java object sorting example (Comparable and Comparator , How do you sort an ArrayList of objects by a property? public static void sort (int [] arr, int from_Index, int to_Index) arr - the array to be sorted from_Index - the index of the first element, inclusive, to be sorted to_Index - the index of the last element, exclusive, to be sorted This method doesn't return any value. A Java program to sort an array of integers in ascending order.

First, the sort operation should be outside the for loop as tyou want to do that only one time. Second, you can sort the list in this way:

memberList.sort(Comparator.comparing(Member::getScore));

If your data are not stored in a List you can sort them in this way:

Arrays.sort(memberList, Comparator.comparingInt(Member::getScore));

Otherwise let your Member class implement Combarable interface.

The Cafes » Why java.util.Arrays uses Two Sorting Algorithms, using natural ordering (alphabetically in ascending order). Lets's write some code for it. In future, Arrays class should provides more generic and handy method – Arrays.sort(Object, String, flag). To sort a object array by its “fruitName” in ascending order. Arrays.sort(fruits, fruitName, Arrays.ASCENDING); To sort a object array by its “quantity” in ascending order. Arrays.sort(fruits, quantity, Arrays.DESCENDING); Reference

Different Approaches to Sorting Elements of an ArrayList in Java , Learn how to sort arrays or primitive types and objects in ascending and descending order. Learn to use Collections.sort () method to sort arraylist of custom objects in java with examples. By default, this method sorts the unsorted list into ascending order, according to the natural ordering of its elements. We can use Collections.reverseOrder () method for reverse sorting. 1.

Java.util.Arrays.sort() Method, The java.util.Arrays.sort(Object[]) method sorts the specified array of Objects into ascending order according to the natural ordering of its elements. Declaration. Sorting of ArrayList in descending order We generally use Collections.sort () method to sort a simple array list. However if the ArrayList is of custom object type then in such case you have two options for sorting- comparable and comparator interfaces.

Sorting Arrays in Java, An array of objects can be sorted using the java.util.Arrays.sort() method with a single argument required i.e. the array to be sorted. A program  Data (Students) Stored in array. To sort an array, we need to do the same thing whatever we have done for sorting an ArrayList (either Implement Comparable or pass a comparator to sort method).

Comments
  • what's the current result? what do you expect instead? further, you probably don't want to do Arrays.sort(memberList, Collections.reverseOrder()); in each iteration. should be after the loop.
  • It's just showing the first member and then breaking with an Exception. I expect that it will print me all the male members only, then sort them into order of their scores and print them again.
  • what exception is that? please include it in your post.
  • Thanks for that. I have tried your Full Code extract but am getting an Exception in thread "main" java.lang.NullPointerException on the if ("male".equals(member.getGender()) line of code.
  • @norniron that means member is null. is it expected for the array memberList to sometimes contain null references? if so then you can prevent it with Arrays.stream(memberList).filter(Objects::nonNull).filter(m -> "male".equals(m.getGender()) && m.getScore() > 50) .... using the last solution of the post. note the addition of .filter(Objects::nonNull). However, if a given member should not be null then you'll need to debug your program and find out the problem.
  • Yes it might. That has sorted it and it is working, @Aomine thanks so much for the detailed help, that's brilliant. Many thanks.
  • I am getting an error with this, Cannot invoke sort(Comparator.comparing(Member::getScore)) on the array type Member[]));