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

  • Home
  • SEARCH
  • 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 7918349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:28:25+00:00 2026-06-03T15:28:25+00:00

I have created a Vector object to store data in Table object as Vector<Table>

  • 0

I have created a Vector object to store data in Table object as Vector<Table>. Vector<Table> contains components as below.

[Vector<Record> records, String tableName, String keyColumnName, int recordCount, int columnCount]

I need to sort tableName in above Vector to my own order and return Vector<Table> with sorted tableNames for other processes.

I have wrote method as below.

private Vector<Table> orderTables(Vector<Table> loadTables) {

    List<String> tableNames = new ArrayList<String>();

    for (Table table : loadTables) {

        String tblName = table.getTableName();
        tableNames.add(tblName);

    }
    Collections.sort(tableNames, new MyComparable());

    return null;
}

But I have no idea about how to write Comparator to this. My own sort order is stored in .properties file. I can read it and get value. But I have no idea about how to compare it.

How could I do it?

  • 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-06-03T15:28:29+00:00Added an answer on June 3, 2026 at 3:28 pm

    Before clarification

    You need to write a Comparator for Table objects that delegates to the tableName‘s comparator:

    new Comparator<Table>() {
        @Override public int compare(Table one, Table two) {
            return one.getTableName().compareTo(two.getTableName());
        }
    }
    

    Note that this will consider Tables that have the same name to be equal. This can mess things up if you put these tables in a HashMap or HashSet. To avoid this, you can detect this case and return one.hashCode() - two.hashCode() if the table names are the same.

    Guava’s ComparisonChain is a convenient way to write such multi-stage comparisons:

    new Comparator<Table>() {
        @Override public int compare(Table one, Table two) {
            return ComparisonChain.start()
                     .compare(one.getTableName(), two.getTableName())
                     .compare(one.hashCode(), two.hashCode())
                     .result();
        }
    }
    

    After clarification

    Okay, the question is to impose a predefined sorting order rather than sorting the Tables by name. In that case, you need to make a Comparator that is aware of the ordering defined in the .properties file.

    One way to achieve this is to initialize a mapping of table names to sorting order indices, and refer that mapping during the comparison. Given the property value:

    SORT_ORDER = SALES,SALE_PRODUCTS,EXPENSES,EXPENSES_ITEMS

    The mapping should look like:

    {
        SALES: 0,
        SALE_PRODUCTS: 1,
        EXPENSES: 2,
        EXPENSES_ITEMS: 3
    }
    

    Here’s what the comparator would look like:

    private static class PredefinedOrderComparator implements Comparator<Table> {
    
        public PredefinedOrderComparator() {
    
            // Initialize orderIndex here
    
        }
    
        private final Map<String, Integer> orderIndex;
    
        @Override public int compare(Table one, Table two) {
            return orderIndex.get(one.getTableName()) - orderIndex.get(two.getTableName());
        } 
    
    }
    

    To populate orderIndex from the property value, you need to:

    1. Get the comma-separated list using getProperty() as you mentioned
    2. Split that value on comma (I recommend using Guava’s Splitter, but String.split or others will work too)
    3. Initialize a new HashMap<String, Integer> and an int index = 0
    4. Iterate through the split tokens, map the current token to index and increment index

    Note the implicit assumption that none of the table names have a comma in it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector for storing the pointers of all objects (created by new
I have created a vector of sets in my program, and I need to
Dear all, this is going to be tough: I have created a game object
I have built a constructor for a matrix object. The data is being stored
I am using a vector of strings in order to store some data in
I have a class A whose objects are created dynamically: A *object; object =
I have a function that creates a 2D vector void generate(int n) { vector<
I have the following function which creates a std::vector of iterators into another container:
I have a std::vector. I want to create iterators representing a slice of that
I have created the following C library for reading an image: typedef struct {

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.