Correct sorting doubles with JTable

I have a table in which the first column contains rows and the second contains doubles.

    stars = new StarDatabase(path);
    Object[][] data = new Object[stars.size()][2];
    int i = 0;
    for (String name : stars.keySet()) {
        data[i][0] = name;
        data[i++][1] = stars.get(name).period;
    }
    StarsTable = new JTable(data, StarsColumnNames);
    StarsTable.setAutoCreateRowSorter(true);

Double elements are sorted as strings, so 1 <15 <2 <25 <3. How can I fix this?

+1
source share
3 answers

The key to this can be found in How to Use Concept Tables: Editors and Renders . Just make sure your second column contains Double. Any of the available methods valueOf()can be used to make the type explicit.

+5
source

Some very good tips here !

+1

GlazedList.. . Glazedlist


+1

Source: https://habr.com/ru/post/1755230/


All Articles