A Funny Java Flavoured Look at the World

Tuesday, November 07, 2006

Sorting a Properties object and Utility classes

I was trying to think of how to sort a Properties object today. I was baffled for a while to find a simple away it was at the end of the day so I went home. Then sometime during the night I remembered the Collections utility class, aha surely this will help sort it. There probably is an easier way to do it but I just couldn't think of one when I was trying but if you know of a better way than I did please add it in the comments.

I sometimes have exactly the same problem using the utility classes at work, I forget to look at them. I think it is a sort developer blind spot, you want some code to do something but its usually one method which wouldn't really fit in a class. I had a similar problem where I wanted to remove a section of text, I started writing a method thinking it would be quick and easy then I realised I had already written a method in a StringsUtils class to do exactly what I wanted and it was tested and working.

I had to do a little bit of tinkering to sort the Properties. In the end I choose to read the keys into a List and then do a Collections.sort. I read this interesting discussion after I googled sorting Properties

here is the code that I came up with. I only sorted the keys because I can then use this to retreive the data from the properties code with it.

<Object> keys = properties.keys();
List <String> elementList = new ArrayList();
while (keys.hasMoreElements()) {


if like me you have trouble putting in examples of the new Java 5 angled brackets into html then I have found use & lt; without the space for opening angled bracket and & gt; without htespace between the & and the gt; will put the closeing bracket it.

Interestingly this is another time that passing the SCJP exam has helped me out during work, albeit not straight away. This was one of the exam gotcha's what does each of the collection words mean because there are three

collections - the term used when talking about collections
Collections - the utility class to sort and do other things to collections
Collection - the interface a lot but not all (Map)

Could that be any more confusing!

If you like laughing then check out my funny blog Amusing IT Stories. which is A blog about funny and amusing stories from the IT environment and the office. It is a mix of news, office humour, IT stories, links, cartoons and anything that I find funny


  • I recently came accross your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.



    By Anonymous Anonymous, at Wed Mar 25, 09:50:00 am 2009  

  • I would rather use a TreeSet to do the sorting.

    Map<String, String> aMap = new HashMap<String, String>();
    Set<String> keys = aMap.keySet();
    TreeSet<String> sortedSet = new TreeSet<String>();

    By Anonymous minus, at Mon Jun 22, 11:38:00 am 2009  

  • My first thought was also to use a TreeSet.

    By Anonymous Anonymous, at Mon Aug 31, 03:36:00 pm 2009  

  • I use this way:

    Properties p...
    Object[] keys = p.keySet().toArray();

    Then you just need to cast keys[x] to String to get property from p. It's concise, isn't it? :-)

    By Anonymous Anonymous, at Sat Dec 31, 11:12:00 am 2011  

Post a Comment

Links to this post:

Create a Link

<< Home