How to sort Properties in java?

ordered properties java
java properties
java properties file
how to sort objects in java
java sorted list
sorting array of objects in java
how to sort two objects in java
java sort list of objects

I have a Properties variable and sometimes I need to add other Properties to it.

Properties myBasicProps = this.getClass.getResourceAsStream(MY_PROPS_PATH);
Properties otherProps = new Properties();
otherProps.load(new StringReader(tempPropsString)); //tempPropsString contains my temporary properties

I want to sort myBasicProps after this. I don't want to get all keys and values, sort them with Collections.sort() and then put it all to a new object. Is there a better way?

No, java.util.Properties extends java.util.Hashtable which doesn't define a predictable sort order for keys or values.

You could try dumping all values into something like java.util.TreeMap, which will impose a natural ordering on your keys.

Sort Properties when saving, Sort Properties when savingTag(s): Language​​ Create a SortedProperties class which extend the regular Properties. Then override the keys() method to return the sorted keys instead. Sorts a property list and turns the sorted list into a string. Sorts property list and print out each key=value pair prepended with specific indentation. Convert a Properties list into a map. An utility class to ease up using property-file resource bundles. Copy a set of properties from one Property to another.

all you have to do is create class which extends Properties. source:

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");, "sorted props");

class SortedProperties extends Properties {
  public Enumeration keys() {
     Enumeration keysEnum = super.keys();
     Vector<String> keyList = new Vector<String>();
     return keyList.elements();


it works for me.

Sort Properties when saving : Properties « Collections « Java Tutorial, Sort Properties when saving : Properties « Collections « Java Tutorial.​fos, "sorted props"); } } class SortedProperties extends Properties { public  For sorting the list with the given property, we use list's sort() method. The sort() method takes the list to be sorted (final sorted list is also the same) and a comparator. In our case, the comparator is a lambda which. takes two objects from the list o1 and o2,

Overriding keys works well with Java 8, but since Java 9, the new implementation of method store don't call the method keys any more but the method entrySet.

So, you have to override entrySet too to make your Properties sorted with Java 8/9/10 when stored.

Here an example with inline overriding:

Properties properties = new Properties() {

    private static final long serialVersionUID = 1L;

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

    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 = Comparator<Map.Entry<Object, Object>>() {

            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());

        while (iterator.hasNext())

        return set2;

    public synchronized Enumeration<Object> keys() {
        return Collections.enumeration(new TreeSet<Object>(super.keySet()));

Sort Properties when saving : Preference Properties « Development , Sort Properties when saving import; import java.util.​Collections; import java.util.Enumeration; import java.util.Properties; import java.​util. You want to render this list, but first you want to order it by the value of one of the properties. For example you want to order it by the color name, in alphabetical order: black, red, white. You can use the sort() method of Array, which takes a callback function,

@danisupr4 has the best solution.

I would improve on it slightly so you won't receive any warnings in your IDE:

public static class MyProperties extends Properties {
    private static final long serialVersionUID = 1L;

    public Enumeration<Object> keys() {
        Enumeration<Object> keysEnum = super.keys();
        Vector<Object> keyList = new Vector<Object>();

        while (keysEnum.hasMoreElements()) {

        Collections.sort(keyList, new Comparator<Object>() {
            public int compare(Object o1, Object o2) {
                return o1.toString().compareTo(o2.toString());

        return keyList.elements();

SortedProperties (Apache Commons Collections 4.4 API), java.util.Properties. Allows other methods on the superclass to work with sorted keys. Since: 4.2; See Also:  Any Java developer worth their salt has used the Collection framework. Here is a simple tutorial that explains how to sort in Java. Join the DZone community and get the full member experience. It

TreeMap should be the simplest way:

Properties myProps = this.getClass.getResourceAsStream(MY_PROPS_PATH);

try {
    myProps.load(new FileInputStream(extraPropertiesFilename));
        //you can load more properties from external file

    Map<String, String> sortedMap = new TreeMap(myProps);

    //output sorted properties (key=value)
    for (String key : sortedMap.keySet()) {
        System.out.println(key + "=" + sortedMap.get(key));

} catch (Exception e) {

How do I list System properties alphabetically?, Description: This Java tutorial shows how to list System properties alphabetically. Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools  First things first. We can’t sort a Java Set collection by calling Collections.sort() method on a Set. That is because most implementations of Java Set doesn’t have a concept of order. A Set is something without an order by definition in Java. In order to sort a Java Set we need to convert it to a list first.

How To Sort Objects In Java, How To Sort Objects In Java Collection framework. Here is a simple tutorial that explains how to sort in Java. by All properties goes here. Comparable is used to sort an arraylist of object on the basis of single property at a time. For example we have student data in which we have two properties, name and age. We can sort it by name or age. This can be done by implementing Comparable interface and overriding its compareTo() method.

Java 8 - Collections sort() method, sort() method to sort arraylist of custom objects in java with examples. By default, this method sorts the unsorted list into ascending order, according to the natural  Learn to use Collections.sort() method to sort arraylist of custom objects in java with examples. By default, this method sorts the unsorted list into ascending order, according to the natural ordering of its elements. We can use Collections.reverseOrder() method for reverse sorting. 1. Sort ArrayList of objects – Collections.sort( list )

Sort a List of Objects by Field in Java - DEV Community ‍ ‍ , Originally published on my blog at Overview Java Tagged with java. Java ArrayList of Object Sort Example (Comparable And Comparator) By Chaitanya Singh | Filed Under: Java Collections In this tutorial we will see how to sort an ArrayList of Objects by property using comparable and comparator interface.

  • @Thillakan i pasted the same link but deleted it after reading "I don't want to get all keys and values, sort them with Collections.sort()..."
  • This will mean upper case first and then lower case.
  • Java 8 version: ArrayList<Object> result = Collections.list(super.keys()); Collections.sort(result, (a, b) -> a.toString().compareTo(b.toString())); return Collections.enumeration(result);
  • This does not work on java >=9 . E.g. when using BB,CC.AA,DD for keys. See my post below.
  • This one was the only one working for me as well with Java 9.