how to sort application properties value

change application properties at runtime spring boot
read value from application.properties spring boot
spring boot dynamic properties
spring boot application.properties example
spring change property value at runtime
load properties file in spring application context
spring boot application.properties not loading
spring boot load properties from database on startup

Is it possible to sort a List interface with object of ArrayList()?

List</*classname*/> list = new ArrayList<>();

I'm using application.properties of Spring boot to set the value in List interface

ModuleNames.java

public class ModuleNames {

    private String moduleId;
    private String moduleName;

    //getters & setters.
}

Controller

@RequestMapping(value = "/getModuleNames", method = { RequestMethod.GET })
public @ResponseBody List<String> getModuleNames() {

        List<ModuleNames> moduleNamesList = new ArrayList<>();
        moduleNamesList.add(new ModuleNames("Country", env.getProperty("bulkUpload.location.country")));
        moduleNamesList.add(new ModuleNames("State", env.getProperty("bulkUpload.location.state")));
        moduleNamesList.add(new ModuleNames("City", env.getProperty("bulkUpload.location.city")));
        moduleNamesList.add(new ModuleNames("Area Pincode", env.getProperty("bulkUpload.location.areaPincode")));

        return moduleNamesList;

does anybody know how to sort the list.

I've tried

  1. moduleNamesList.sort(Comparator.naturalOrder());

  2. Collections.sort(moduleNamesList);

but both doesn't seem to work. Please help me here

You can avoid defining the comparison logic by using an instance method reference and the Comparator.comparing method – which extracts and creates a Comparable based on that function.

We're going to use the getters to build the Lambda expression.

sort the list by moduleName:

moduleNamesList.sort(Comparator.comparing(ModuleNames::getModuleName));

sort the list by moduleName in reverse order:

moduleNamesList.sort(Comparator.comparing(ModuleNames::getModuleName).reversed());

sort the list by first moduleName and then moduleId:

moduleNamesList.sort(Comparator.comparing(ModuleNames::getModuleName).thenComparing(ModuleNames::getModuleId));

You can also use custom comparator:Use This Reference

Spring boot property resolution order., Property values can be injected directly into your beans using the @Value annotation, Profile-specific application properties outside of your packaged jar​  eTour.com is the newest place to search, delivering top results from across the web. Content updated daily for value of a property.

You need to write your custom comparator for ModuleNames object, because Java doesn't know how to sort it as it is not a primitive type.

For example:

Collections.sort(moduleNamesList, new Comparator<ModuleNames>() {
        @Override
        public int compare(ModuleNames o1, ModuleNames o2) {
            return o1.getModuleId().compareTo(o2.getModuleId());
        }
    });

or

moduleNamesList.sort(new Comparator<ModuleNames>() {
        @Override
        public int compare(ModuleNames o1, ModuleNames o2) {
            return o1.getModuleName().compareTo(o2.getModuleName());
        }
    });

59. Properties & configuration, 59.2 Change the location of external properties of an application Chapter 21, Externalized Configuration in the “Spring Boot features” section for the exact order). If a YAML document contains a spring.profiles key, then the profiles value  Just Enter your Zip Code for Property Valuations in Your Area. Expert Results, for Free. Free Comprehensive Details on Homes & Property Near You. Start Searching Now!

moduleNamesList.sort(Comparator.comparing(ModuleNames::getName));
moduleNamesList.sort(Comparator.comparing(mn -> mn.name));

Or even more sorted:

moduleNamesList.sort(Comparator.comparing(ModuleNames::getName)
                     .thenComparing(ModuleNames::getId));

Spring Boot: Configuring Properties, If there is the same key-value present both in application.properties and arguments, etc. The order in which Spring considers it can be found here. 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, which takes as parameters 2 objects contained in the array (which we call a and b ):

The @Value Annotation in Spring, Value is a Java annotation used at the field or method/constructor level. We will use the default property file for Spring Boot - application.properties , where we  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 (The Java™ Tutorials > Essential Classes > The Platform , Properties are configuration values managed as key/value pairs. Next, the application creates another Properties object and loads the properties that were  An important application related to sorting is the operation of finding the median of a set of keys (the value with the property that half the keys are no larger and half the keys are no smaller). This operation is a common computation in statistics and in various other data-processing applications.

Oracle Commerce Guided Search, the value of the descending property can be either true or false, to specify To enable your application to provide such choices through a drop down list or other​  public Microsoft.Office.Interop.Excel.Sort Sort { get; } Property Value Sort. A Microsoft.Office.Interop.Excel.Sort that represents the sort column or columns, and the sort order for the ListObject collection. Examples. The following code example adds a ListObject to the current worksheet.

Comments
  • Do you want to sort moduleNamesList?
  • on what basis.... moduleId or moduleName ?
  • @Nitika on moduleName
  • check my answer