Tyluur
Tyluur

Reputation: 91

Sort JList by name

What I am doing is getting elements from a map and adding them onto a JList to display on a GUI. I want to know how to sort the names alphabetically.

private void refreshShopsList() {
    gameShopsJList.setModel(new javax.swing.AbstractListModel<String>() {

        public int getSize() {
            return ShopsLoader.getShops().size();
        }

        public String getElementAt(int i) {
            return getShopByIndex(i).getName();
        }
    });     
}

private Shop getShopByIndex(int index) {
    Iterator<Entry<String, Shop>> it = ShopsLoader.getShops().entrySet().iterator();
    int count = -1;
    while(it.hasNext()) {
        Entry<String, Shop> entry = it.next();
        count++;
        if (count == index)
            return entry.getValue();
    }
    return null;
}

/**
 * The map of the shops
 */
private static final Map<String, Shop> shops = new HashMap<String, Shop>();

public static Map<String, Shop> getShops() {
    return shops;
}

Upvotes: 0

Views: 6496

Answers (2)

Patrick
Patrick

Reputation: 4572

Here is a little example, which sorts your shop names.

The ShopComparator class does the sorting task:

package model;

import java.util.Comparator;

public class ShopComparator implements Comparator<Shop> {

    @Override
    public int compare(Shop o1, Shop o2) {
        return o1.getName().compareTo(o2.getName());
    }

}

The Shop class, as simple as possible:

package model;

public class Shop {

    private int id;
    private String name;

    public Shop(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

And the main app:

package model;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.TreeSet;

public class App {

    public static void main(String[] args) {
        Map<String, Shop> shops = new HashMap<String, Shop>();
        Shop s1 = new Shop(1, "Apus Drugstore");
        Shop s2 = new Shop(2, "DM");
        Shop s3 = new Shop(3, "Kaufhof");
        Shop s4 = new Shop(4, "Moes Traverne");

        shops.put("one", s3);
        shops.put("two", s4);
        shops.put("three", s1);
        shops.put("four", s2);

        for(Shop s : shops.values()) {
            System.out.println(s.getName());
        }
        ShopComparator sc = new ShopComparator();
        TreeSet<Shop> sortedShops = new TreeSet<>(sc);

        sortedShops.addAll(shops.values());

        for(Shop s : sortedShops) {
            System.out.println(s.getName());
        }
    }

}

First output, unsorted: Moes Traverne Kaufhof Apus Drugstore DM

and the sorted output.

Apus Drugstore DM Kaufhof Moes Traverne

Upvotes: 1

Sergey Fedorov
Sergey Fedorov

Reputation: 2169

Algorithm:

  1. get all values from JList, convert them to strings, store in array
  2. sort the array
  3. set new values to JList.

code:

    JList jl = new JList(new Object[]{4.5,1,"Hi!"});
    ListModel model = jl.getModel();
    String[] strings = new String[model.getSize()];
    for(int i=0;i<strings.length;i++){
        strings[i]=model.getElementAt(i).toString();
    }
    Arrays.sort(strings);
    jl.setListData(strings);    

see about Comparator if you need to sort array in any other order.

Upvotes: 0

Related Questions