0

my arraylist :

"LVL:100 MONEY:1489 MANA: 42,1",
"LVL:67 MONEY:389 MANA: 33,5",
"LVL:47 MONEY:4229 MANA: 59,7",
"LVL:120 MONEY:1189 MANA: 94,5",
"LVL:150 MONEY:189 MANA: 19,2",

so I want it to sorted by value of MONEY

"LVL:47 MONEY:4229 MANA: 59,7",
"LVL:100 MONEY:1489 MANA: 42,1",
"LVL:120 MONEY:1189 MANA: 94,5",
"LVL:67 MONEY:389 MANA: 33,5",
"LVL:150 MONEY:189 MANA: 19,2",
6
  • what is the structure of your object , it is List<SomeObject> , what is that object ? Commented Jan 13, 2022 at 2:57
  • its list of String ArrayList<String> playerstats = new ArrayList<String>(); Commented Jan 13, 2022 at 3:00
  • please share your main method and how you are populating the values in the playerstats. Commented Jan 13, 2022 at 3:01
  • maybe I should use json,but I dont know how to assign the child of the value Commented Jan 13, 2022 at 3:02
  • I get the value from a jsonObject from url string playerstats = rootobj.get("stats").getAsJsonObject(); LVL= playerstats.get("lvl").getAsInt() MONEY= playerstats.get("money").getAsInt() MANA= playerstats.get("mana").getAsInt() then return it as string "LVL:" + LVL + " MONEY:"+MONEY + " MANA:"+MANA <-- added to arraylist Commented Jan 13, 2022 at 3:10

1 Answer 1

3

Here is my attempt using Streams API. I make a new Comparator to give to sorted method in order to sort the money based on where it appears in the string.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SortMoney {

    public static void main(String[] args) {
    List<String> playerstats = Arrays.asList("LVL:100 MONEY:1489 MANA: 42,1",
                                             "LVL:67 MONEY:389 MANA: 33,5",
                                             "LVL:47 MONEY:4229 MANA: 59,7",
                                             "LVL:120 MONEY:1189 MANA: 94,5",
                                             "LVL:150 MONEY:189 MANA: 19,2");

    List<String> sortedPlayerStates = playerstats.stream()
                                                 .map(data -> data.split(":"))
                                                 .sorted((o1, o2) -> {
                                                     int a = Integer.parseInt(o1[2].split(" ")[0]);
                                                     int b = Integer.parseInt(o2[2].split(" ")[0]);
                                                     if (a > b) {
                                                     return -1;
                                                     } else if (b > a) {
                                                     return 1;
                                                     } else {
                                                     return 0;
                                                     }

                                                 })
                                                 .map(data -> String.join(":", data))
                                                 .collect(Collectors.toList());

    sortedPlayerStates.forEach(System.out::println);
    }

}

Output: enter image description here

Let me know if this is the result you want.

Edit: changed the code to store in variable in case you want to manipulate it later.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.