Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7064055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:47:15+00:00 2026-05-28T04:47:15+00:00

How do I pull the reflection out of this loop and pass in the

  • 0

How do I pull the reflection out of this loop and pass in the getter method if that’s possible.

public <E> void sortBy(final String fieldName, final boolean sortAsc, List<E> list){
        Collections.sort(list, new Comparator<E>() {
                public int compare(E o1, E o2) {
                    return compareFields(o1,o2,fieldName.replace("SortBy", "get"),sortAsc);
                }
            }
        );
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected <E> int compareFields(E o1, E o2,String fieldName, boolean sortAsc){
        try { 
            Comparable o1Data;
            Comparable o2Data;
            o1Data = (Comparable) o1.getClass().getMethod(fieldName).invoke(o1);
            o2Data = (Comparable) o2.getClass().getMethod(fieldName).invoke(o2);
            if(o1Data == null && o2Data == null){
                return 0;
            } else if (o1Data == null){
                return 1;
            } else if (o2Data == null){
                return -1;
            }
            int result = o2Data.compareTo(o1Data); 
            return (sortAsc) ? -result : result ; 
        }
        catch(Exception e) {
            throw new RuntimeException(e);
        }
    }

Context: I’ve got many screens with data tables. Each one is build from a List. Each data table needs to be sortable by each of its 6 columns. The columns are either Date or String.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:47:15+00:00Added an answer on May 28, 2026 at 4:47 am

    If you can assume all the elements are the same type.

    public <E> void sortBy(final String fieldName, final boolean sortAsc, List<E> list) throws NoSuchMethodException {
        final Method f = list.get(0).getClass().getMethod(fieldName.replace("SortBy", "get"));
        f.setAccessible(true);
        final int direction = sortAsc ? +1 : -1;
        Collections.sort(list, new Comparator<E>() {
            public int compare(E o1, E o2) {
                return compareFields(o1, o2, f, direction);
            }
        }
        );
    }
    
    @SuppressWarnings({"rawtypes", "unchecked"})
    protected <E> int compareFields(E o1, E o2, Method getter, int sortAsc) {
        try {
            Comparable o1Data = (Comparable) getter.invoke(o1);
            Comparable o2Data = (Comparable) getter.invoke(o2);
            if (o1Data == null)
                return o2Data == null ? 0 : 1;
            if (o2Data == null)
                return -1;
            return sortAsc * o2Data.compareTo(o1Data);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a generic class that I am using Reflection to pull out the
I want to pull out duplicate records in a MySQL Database. This can be
I have some code that is using reflection to pull property values from an
I am starting to pull my hair out over this: I am trying to
How do I pull out the filename from a full path using regular expressions
I want to pull a series of relationships out of xml files and transform
Is it possible to pull a bunch of .jpg pictures form a local file
In my plugin architecture I am currently passing a plugin name (string), method name
I am trying to pull dynamics from a load that I run using bash.
I pull json data from a server. It contains a dictionary with text that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.