Re: How to sort these Strings?

Discussion in 'Java' started by RedGrittyBrick, Aug 7, 2008.

  1. www wrote:
    > Hi,
    >
    > I have a TreeMap:
    >
    > final TreeMap<String, String> treeMap = new TreeMap<String,
    > String>(getMap());
    > final Set<Map.Entry<String, String>> mySet = treeMap.entrySet();
    >
    > for (final Map.Entry<String, String> curEntry : mySet)
    > {
    > resultStr.append(curEntry.getKey() + "=" +
    > curEntry.getValue() + LINE_END);
    > }
    >
    > It prints out in an order based on the key(String) alphabetic order,
    > which is what I want. The only problem is that sometimes, the keys are:
    >
    > STATE_0, STATE_1, .. , STATE_10, ..., STATE_20
    >
    > Now, they are not printed out the order I want. How can I make it print
    > out the order:
    >
    > STATE_0
    > STATE_1
    > ...
    > STATE_10
    > ..
    >
    > Thank you very much.


    Create a suitable comparator (e.g. using String#split) and pass it to
    the TreeSet constructor.

    <http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeMap.html#TreeMap(java.util.Comparator)>
    (or http://preview.tinyurl.com/3xtsqc or http://tinyurl.com/3xtsqc)

    I recall a similar question recently. If you use Google to search
    Groups for TreeMap, in the last month in comp.lang.java.programmer
    you'll find discussion and examples of this.

    --
    RGB
     
    RedGrittyBrick, Aug 7, 2008
    #1
    1. Advertising

  2. RedGrittyBrick

    Lew Guest

    On Aug 7, 9:41 am, RedGrittyBrick <>
    wrote:
    > www wrote:
    > > Hi,

    >
    > > I have a TreeMap:

    >
    > >         final TreeMap<String, String> treeMap = new TreeMap<String,
    > > String>(getMap());
    > >         final Set<Map.Entry<String, String>> mySet = treeMap.entrySet();

    >
    > >         for (final Map.Entry<String, String> curEntry : mySet)
    > >         {
    > >             resultStr.append(curEntry.getKey() + "=" +
    > > curEntry.getValue() + LINE_END);
    > >         }

    >
    > > It prints out in an order based on the key(String) alphabetic order,
    > > which is what I want. The only problem is that sometimes, the keys are:

    >
    > > STATE_0, STATE_1, .. , STATE_10, ..., STATE_20

    >
    > > Now, they are not printed out the order I want. How can I make it print
    > > out the order:

    >
    > > STATE_0
    > > STATE_1
    > > ...
    > > STATE_10
    > > ..

    >
    > > Thank you very much.

    >
    > Create a suitable comparator (e.g. using String#split) and pass it to
    > the TreeSet constructor.
    >
    > <http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeMap.html#TreeMa...)>
    > (orhttp://preview.tinyurl.com/3xtsqcorhttp://tinyurl.com/3xtsqc)
    >
    > I recall a similar question recently. If you use Google to search
    > Groups for TreeMap, in the last month in comp.lang.java.programmer
    > you'll find discussion and examples of this.


    And careful perusal of the Javadocs for TreeMap would also have
    helped, were one to have tried it.

    --
    Lew
     
    Lew, Aug 7, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    0
    Views:
    538
    nobody
    Jun 1, 2004
  2. JerryJ
    Replies:
    11
    Views:
    1,406
    Dave Moore
    Apr 28, 2004
  3. KraftDiner

    How do I sort these?

    KraftDiner, Oct 28, 2005, in forum: Python
    Replies:
    15
    Views:
    423
    Bengt Richter
    Oct 30, 2005
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    766
    Malcolm
    Jun 24, 2006
  5. Navin
    Replies:
    1
    Views:
    698
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page