2

I'm making a minecraft mod menu (That sounds even worse on here) and Im trying to make the string variables appear from most lengthy to least lengthy I.e: "Chest Stealer" "Player ESP" "Flight" "Speed" "box" etc.

Here is my current code:

for(Module m : myClient.getModules())
    {
        if(m.isToggled() && m.getName() != "Click GUI" )
        {
            modules.add(m.getName());
            for(int i = 0; i < modules.size();i++)
            {
            int currentModule = i;
            for(int j = 0; j < modules.size();j++)
            {
                if(modules.get(currentModule).length() > modules.get(j).length())
                {
                    int currentPos;
                    currentPos = modules.indexOf(i);
                    modules.set(modules.indexOf(j), modules.get(i));
                    modules.set(currentPos, modules.get(j));
                }
            }

Every time I try and tackle this problem I end up forgetting what I wanted to do mid-coding or it just doesnt work. Current, while 1 mod is active: Draws the mods name, no problem 2 mods are active: Leaves the mod that was activated first on top and draws the second mod's name twice below. 3+ mods are active: crashes.

Any feedback will be greatly appreciate, Thanks everyone

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Christian
  • 52
  • 1
  • 13
  • 2
    I think you would benefit from the [Sorting a List using Comparable or a Comparator](http://stackoverflow.com/documentation/java/3137/comparable-and-comparator/10693/sorting-a-list-using-comparablet-or-a-comparatort#t=201608131317475628656) example in [Documentation](http://stackoverflow.com/documentation)... – ppeterka Aug 13 '16 at 13:19
  • Why are you using `indexOf`? – Oliver Charlesworth Aug 13 '16 at 13:21
  • Make the thing you want to sort implement `Comparable`. Read https://docs.oracle.com/javase/tutorial/collections/interfaces/order.html. – user1803551 Aug 13 '16 at 13:22
  • `m.getName() != "Click GUI"` is also incorrect. See [How do I compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Jorn Vernee Aug 13 '16 at 13:25

2 Answers2

3

Here is a solution using streams:

List<String> modules = myClient.getModules().stream()
    .filter(m -> m.isToggled() && !"Click GUI".equals(m.getName()))
    .map(Module::getName)
    .sorted(Comparator.comparing(String::length).reversed())
    .collect(Collectors.toList());

This assumes getModules return some collection that can be streamed.

Jorn Vernee
  • 31,735
  • 4
  • 76
  • 93
2

You need to write a module comparator by implemening java.util.Comparator interface and write your sorting logic in it, like:

import java.util.Comparator;

public class ModuleSorter implements Comparator<Module> {

    @Override
    public int compare(Module module1, Module module2) {
        return Integer.valueOf(module2.getName().length()).compareTo(module1.getName().length());
    }

}

I am assuming that myClient.getModules() returns an implementation of java.util.List. The sorting now would be as simple as:

final List<Module> moduleList = myClient.getModules();
moduleList.sort(new ModuleSorter());

The modules inside moduleList are sorted as per your requirement.

Ajay Deshwal
  • 1,286
  • 13
  • 21