1

Is there a way to sort the Properties object in java?

I have the string which groups the Properties and checks whether the data is available in the map format.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97

3 Answers3

3

You can find an example without sub-classing Properties and working with Java 8/9/10

By this way, keySet, keys and entrySet methods from Properties return sorted keys. Then method store save the properties file sorted too.

This code is here

Properties properties = new Properties() {

private static final long serialVersionUID = 1L;

@Override
public Set<Object> keySet() {
    return Collections.unmodifiableSet(new TreeSet<Object>(super.keySet()));
}

@Override
public Set<Map.Entry<Object, Object>> entrySet() {

    Set<Map.Entry<Object, Object>> set1 = super.entrySet();
    Set<Map.Entry<Object, Object>> set2 = new LinkedHashSet<Map.Entry<Object, Object>>(set1.size());

    Iterator<Map.Entry<Object, Object>> iterator = set1.stream().sorted(new Comparator<Map.Entry<Object, Object>>() {

        @Override
        public int compare(java.util.Map.Entry<Object, Object> o1, java.util.Map.Entry<Object, Object> o2) {
            return o1.getKey().toString().compareTo(o2.getKey().toString());
        }
    }).iterator();

    while (iterator.hasNext())
        set2.add(iterator.next());

    return set2;
}

@Override
public synchronized Enumeration<Object> keys() {
    return Collections.enumeration(new TreeSet<Object>(super.keySet()));
    }
};
Stéphane Millien
  • 3,238
  • 22
  • 36
  • 2
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – Zoe Sep 01 '18 at 12:01
  • thx, this works except for numeric keys i.e property10 would come before property2 – Paul Taylor Apr 27 '21 at 09:03
0

Please use this example :

from the link : http://www.java2s.com/Tutorial/Java/0140__Collections/SortPropertieswhensaving.htm

import java.io.FileOutputStream;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

public class Main{
  public static void main(String[] args) throws Exception {
    SortedProperties sp = new SortedProperties();
    sp.put("B", "value B");
    sp.put("C", "value C");
    sp.put("A", "value A");
    sp.put("D", "value D");
    FileOutputStream fos = new FileOutputStream("sp.props");
    sp.store(fos, "sorted props");
  }

}
class SortedProperties extends Properties {
  public Enumeration keys() {
     Enumeration keysEnum = super.keys();
     Vector<String> keyList = new Vector<String>();
     while(keysEnum.hasMoreElements()){
       keyList.add((String)keysEnum.nextElement());
     }
     Collections.sort(keyList);
     return keyList.elements();
  }

}
Harry
  • 3,072
  • 6
  • 43
  • 100
-1

Consider that TreeMap is a sorted Map, then you can do:

//properties as they are:
System.out.println(System.getProperties());

//now sorted:
TreeMap<Object, Object> sorted = new TreeMap<>(System.getProperties());
System.out.println(sorted);
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97