Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:22:29+00:00 2026-06-15T10:22:29+00:00

I have an ArrayList of Player(s). The array is currently ordered in the player

  • 0

I have an ArrayList of Player(s). The array is currently ordered in the player who’ve been logged on first, to the most recent one.

However, i want to sort the ArrayList from the Players positions, high to low. The position is stored in the Player class, retrieved by

player.getX();

I imagine i should have a loop with each item being added to a new ArrayList, but i can’t quite figure out how to actually implement it.

How can i do this?

Loop:

for (int i = 0; i < otherPlayers.size(); i++) {
    Player currentPlayer = otherPlayers.get(i);
}

Edit: Added loop example

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:22:30+00:00Added an answer on June 15, 2026 at 10:22 am

    Either you make Player implementing Comparable and use Collections.sort(players) or you can directly create a comparator like this

        Collections.sort(players, new Comparator<Player>() {
            public int compare(Player o1, Player o2) {
                return o2.getX().compareTo(o1.getX());
            }
        });
    

    Assuming player.getX() returns a Integer

    Edit

    Let’s say you have your List<Player> named otherPlayers if you want to sort it directly without writing yourself a loop and the algorithm of sorting, you can rely on the Java built-in method Collections.sort() to sort it. But this means that either your class Person implements the Comparable interface, or you can pass an instance of Comparator as the second argument of the Collections.sort() method as done above.

    The Comparator.compare() method takes two object and does return an integer that says if the first argument is lower than the second, equals or greater. This is how the sorting algorithm will know how to order the elements in your list. So, by creating a Comparator instance that compare the result of Player.getX() of two Player instances (the elements of your list), you are able to sort the original list on the position criteria as you asked for it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array list of objects in my application. private static ArrayList<Player> userList=new
I have arraylist myorderdata . I want to retrieve this arraylist to one String
I want to pass a ArrayList from one activity to other. I have found
I have two arraylist with a number of model objects.I want to find the
I have an arraylist of entites that I want to move randomly. No matter
I have an arrayList of object Player. Player has field id, and score. suppose
I have an ArrayList that contains Objects. What I have been wanting/needing to do
I currently have an arraylist in which I add blossom objects ( which fall
I currently have an arrayList() called players in JavaScript in which I push data
At the moment inside the class I have ArrayList which the objects are stored

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.