1 2 3 Previous Next 69 Replies Latest reply: Mar 18, 2010 8:45 PM by PhHein RSS

    Problem with sorting String values

    807580
      Hello.
      I have float values, which I format on 2 decimal places using DecimalFormat :
      new java.text.DecimalFormat("##.##").format(val1/val2*100)
      I add *"%"* symbol and set it to JTable as value
      table.setValueAt(new java.text.DecimalFormat("##.##").format(val1/val2*100)+" %", row, column);
      Values are from 0 to 100%, works good

      When I try to sort them - I fail.
      Example : values 15,8,9 are sorted as : 9,8,15 (first number, sorting as strings)

      How can I sort them correctly?

      I tried returning Object.class and Float.class and String.class for JTable column and non helped.

      Thanks.

      Edited by: Falkenbach on Mar 16, 2010 7:35 AM

      Edited by: Falkenbach on Mar 16, 2010 7:36 AM
        • 1. Re: Problem with sorting String values
          807580
          Save your values as double/float values, and use a renderer to display them in the format you want. Google for a cell renderer tutorial. The Sun tutorial is pretty good.
          • 2. Re: Problem with sorting String values
            807580
            codingMonkey wrote:
            Save your values as double/float values, and use a renderer to display them in the format you want. Google for a cell renderer tutorial. The Sun tutorial is pretty good.
            I took a look on that, but still didn't work

            please take a look at code I used
            public class PercentageCellRenderer extends DefaultTableCellRenderer {
                public PercentageCellRenderer() {
                    super();
                }
                @Override
                public void setValue(Object value) {
                    setText((value == null) ? "" : value + " %");
                }
            }
            table.getColumnModel().getColumn(11).setCellRenderer(new PercentageCellRenderer());
            Now I setValue in table with pure float

            I am afraid I didnt change DISPLAYING at all, I changed value to String again ....

            Edited by: Falkenbach on Mar 16, 2010 8:11 AM
            • 3. Re: Problem with sorting String values
              807580
              If you wish to sort as a String value, then you need to keep in mind how string values sort--by character by character comparison starting at the left most:

              (1, 3, 5, 11, 14, 22) will sort as string:

              ("1", "11", "14", "22", "3", "5") to get the to sort properly, you need to format them with leading zeros:

              ("01", "03", "05", "11", "14", "22") sorted as String with leading zeros.
              • 4. Re: Problem with sorting String values
                807580
                morgalr wrote:
                If you wish to sort as a String value, then you need to keep in mind how string values sort--by character by character comparison starting at the left most:

                (1, 3, 5, 11, 14, 22) will sort as string:

                ("1", "11", "14", "22", "3", "5") to get the to sort properly, you need to format them with leading zeros:

                ("01", "03", "05", "11", "14", "22") sorted as String with leading zeros.
                13 will be sorted higher than 123 :-(
                • 5. Re: Problem with sorting String values
                  796262
                  Falkenbach wrote:
                  morgalr wrote:
                  If you wish to sort as a String value, then you need to keep in mind how string values sort--by character by character comparison starting at the left most:

                  (1, 3, 5, 11, 14, 22) will sort as string:

                  ("1", "11", "14", "22", "3", "5") to get the to sort properly, you need to format them with leading zeros:

                  ("01", "03", "05", "11", "14", "22") sorted as String with leading zeros.
                  13 will be sorted higher than 123 :-(
                  But 013 won't. If you want further help, you'll have to provide an SSCCE .
                  • 6. Re: Problem with sorting String values
                    807580
                    013% looks like crap :)

                    I dont think there ist need for SSCCE, I provided relevant code.
                    And besides, dragging swing JTable component on frame with some GUI builder is much more faster, then just set values to some strings I showed and see yourself ...
                    • 7. Re: Problem with sorting String values
                      darrylburke
                      dragging swing JTable component on frame with some GUI builder is much more faster
                      Oh, you're one of those :-\
                      • 8. Re: Problem with sorting String values
                        DrClap
                        Falkenbach wrote:
                        I dont think there ist need for SSCCE, I provided relevant code.
                        You appear to be storing the data in your table model as numeric data. That's the only relevant code I saw. I didn't see the code which was sorting the data. That would be relevant code too, since you were asking about sorting. Rendering code is irrelevant.

                        You'll want to override the getColumnClass() method of your table model to return something other than String, if you're allowing someone else's code to sort the table's data. That would be relevant code if you had done that.
                        • 9. Re: Problem with sorting String values
                          807580
                          Falkenbach wrote:
                          013% looks like crap :)

                          I dont think there ist need for SSCCE, I provided relevant code.
                          And besides, dragging swing JTable component on frame with some GUI builder is much more faster, then just set values to some strings I showed and see yourself ...
                          Either I'm missing the point or you are. As others have said, it sounds to me like you need to keep the numbers as double (or float maybe) and sort on these values. When displaying one uses a renderer to display the double exactly as one wants.

                          As to whether or not there is a need for an SSCCE. I, in common with at least one other, think there is since you seem to have ignored the advice to keep the values and doubles to aid sorting and an SSCCE would help us to understand why you have ignored the advice. Your problem, your call.

                          Bye
                          • 10. Re: Problem with sorting String values
                            796440
                            Falkenbach wrote:
                            I dont think there ist need for SSCCE,
                            No, you're right, there's no need for one. However, the people whose help you seek have asked for one, and if you don't provide it, they will very likely simply choose not to help. So you can choose not to provide one because you want to be right, but if your goal is to actually get help, then it's in your own best interest to make it as easy as possible for people to help you. If that means that you have to do work to provide an SSCCE so that we don't have to do work to try to understand your code, then so be it.

                            The choice is yours.
                            • 11. Re: Problem with sorting String values
                              807580
                              Gentlemen, you seem really pissed off.
                              So let me exaplain some things :
                              1. I use swing builder in NetBeans, so no I dont write whole JTable component myself - I am comfortable wtih one provided
                              2. Sorting data is already implemented in JTable -
                              table.setAutoCreateRowSorter(true);
                              And sorting is correct, because I dont know how to store data in to JTable as floats and display them as Strings, therefore I can't provide SSCE because I do not use custom renderers except one I pasted ...

                              I can store them as floats - sorting works good, but once I add "%" character, sorting fails because they become Strings

                              Edited by: Falkenbach on Mar 16, 2010 12:33 PM

                              Edited by: Falkenbach on Mar 16, 2010 12:34 PM
                              • 12. Re: Problem with sorting String values
                                DrClap
                                Falkenbach wrote:
                                1. I use swing builder in NetBeans, so no I dont write whole JTable component myself - I am comfortable wtih one provided
                                You're comfortable with how it works? Then why are you here asking questions about it?
                                2. Sorting data is already implemented in JTable -
                                table.setAutoCreateRowSorter(true);
                                I can store them as floats - sorting works good, but once I add "%" character, sorting fails because they become Strings
                                Okay. So you'll need to provide a renderer for that column which renders the value in a suitable way. We've been through that already in this thread. If simply applying a renderer breaks the sorting, then you're going to have to learn more about JTable (or get NetBeans to fix it for you), but I don't believe it should.
                                • 13. Re: Problem with sorting String values
                                  807580
                                  Okay. So you'll need to provide a renderer for that column which renders the value in a suitable way. We've been through that already in this thread. If simply applying a renderer breaks the sorting, then you're going to have to learn more about JTable (or get NetBeans to fix it for you), but I don't believe it should.
                                  Yes we have.
                                  And I tried and posted one, which didn't solve the matter.
                                  And I asked, what is wrong with it (post number 2)
                                  • 14. Re: Problem with sorting String values
                                    DrClap
                                    Falkenbach wrote:
                                    Okay. So you'll need to provide a renderer for that column which renders the value in a suitable way. We've been through that already in this thread. If simply applying a renderer breaks the sorting, then you're going to have to learn more about JTable (or get NetBeans to fix it for you), but I don't believe it should.
                                    Yes we have.
                                    And I tried and posted one, which didn't solve the matter.
                                    And I asked, what is wrong with it (post number 2)
                                    Okay. So what does NetBeans have to say about that?

                                    I suppose nothing. So now it's up to you to learn about JTables for yourself. Unless you want to post that SSCCE you keep claiming is unnecessary.
                                    1 2 3 Previous Next