Uploaded image for project: 'Commons BeanUtils'
  1. Commons BeanUtils
  2. BEANUTILS-352

Sorting the collection based on bean property in "CollectionUtils"

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 2.0.0
    • Component/s: Bean-Collections
    • Labels:
      None
    • Environment:

      JDK 1.5 and above , Apache Collections

      Description

      Hi Folks ,

      Thanks for your good work done in Collection Framework .

      It would be good if you add functionality to sort the collection based on bean's property name . ( CollectionsUtils,java)

      For example i have List<Person> , i should be able to sort (ascending or desending ) based on person's salary (or) firstName (or) lastName (or) may be the combination of above

      below is the equivalent in SQL

      (select * from person order by salary,firtsName,lastName asc )

      method looks like

      public static <T> void sort(List<T> list, boolean isAscending,String... beanPropertyName) {
      Collection<Comparator<T>> beanComparatorCollection = new ArrayList<Comparator<T>>(beanPropertyName.length);
      for(int i =0;i<beanPropertyName.length;i++)

      { beanComparatorCollection.add(new BeanComparator(beanPropertyName[i])); }

      Comparator<T> finalComparator = ComparatorUtils.chainedComparator(beanComparatorCollection);
      if (!isAscending)

      { finalComparator = new ReverseComparator(finalComparator); }

      Collections.sort(list, finalComparator);
      }

      Please let me know if you need any details , infact i able to submit patch too .

        Attachments

        1. CollectionsUtilExt.java
          15 kB
          Ganesh Gowtham
        2. CollectionsUtilExtTestCase.java
          5 kB
          Ganesh Gowtham

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              ganesh.gowtham Ganesh Gowtham
            • Votes:
              2 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: