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 8465453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:03:07+00:00 2026-06-10T15:03:07+00:00

I have two lists where the order of one list is different from the

  • 0

I have two lists where the order of one list is different from the order of other list, in the list I am adding a map where key order is different in both lists. I want the order of both the list same. How can I perform this?

Map<Long, String> attfieldMap = null;
attfieldMap = view.getNameMap(form);

List<Field> auditMap = new ArrayList<Field>();
if (itemId != 0) {

    Item dom = getRecord();

    Map<FormField, Object> tempValues = dom.getValues();
    List<Field> oldValues = new ArrayList<Field>();

    for (Map.Entry<FormField, Object> values : tempValues
            .entrySet()) {
        Field oldFld = new Field();
        Form form = (Form) values.getKey();

        if (attfieldMap.get(form.getField().getId()) != null) {
            oldFld.setKey(attfieldMap.get(form.getField()
                    .getId()));
            oldFld.setValue(values.getValue());

            oldValues.add(oldFld);
        }
    }
}


for (Map.Entry<Long, String> attfieldEntry : attfieldMap.entrySet()) {
    // Mandates to declare within loop
    Field attributeFld = new Field;

    attributeFld.setKey(attfieldEntry.getValue());
    attributeFld.setValue(String.valueOf(attfieldEntry.getKey()));

    auditMap.add(attributeFld);
}

The list auditMap has map with key att13, att12, att14 and oldValues has map with key att12, att13, att14. I need this list to be in same order as auditMap list. How to order this?

As per the answeres the below code edited :Here the data gets copied,iwant just the sort,even though the list size is different.list1 should have its own data,list2 should have same data,but the order should be maintained

List<Field> auditMap = new ArrayList<Field>();
            attfieldMap = View
                    .getFieldID(form);

            for (Map.Entry<Long, String> attfieldEntry : attfieldMap.entrySet()) {

                Field attributeFld = new Field();

                attributeFld.setKey(attfieldEntry.getValue());
                attributeFld.setValue(String.valueOf(attfieldEntry.getKey()));

                auditMap.add(attributeFld);
                attributeFld = null;
            }

            if (itemId != 0) {

                Item dom = getRecord(domainItem);

                Map<FormField, Object> tempValues = dom.getValues();
                List<Field> oldValues = new ArrayList<Field>();

                for (Map.Entry<FormField, Object> values : tempValues
                        .entrySet()) {
                    Field oldFld = new Field();
                    Form form = (Form) values.getKey();

                    if (attfieldMap.get(form.getField().getId()) != null) {
                        oldFld.setKey(attfieldMap.get(form.getField()
                                .getId()));
                        oldFld.setValue(values.getValue());

                        oldValues.add(oldFld);
                    }

                }
                final Map<Field, Integer> indices = new HashMap<Field, Integer>();
                for (int i = 0; i < oldValues.size(); i++) {
                    indices.put(oldValues.get(i), i);
                }
                Collections.sort(auditMap, new Comparator<Field>() {
                    public int compare(Field o1, Field o2) {
                        int index1 = indices.containsKey(o1) ? indices.get(o1)
                                : -1;
                        int index2 = indices.containsKey(o2) ? indices.get(o2)
                                : -1;
                        return index1 - index2;
                    }
                });
                for (int i = 0; i < oldValues.size(); i++) {
                    LOGGER.info("the new data is--" + oldValues.get(i).getKey());
                }
                for (int i = 0; i < auditMap.size(); i++) {
                    LOGGER.info("the new data is--" + auditMap.get(i).getKey());
                }

This is my actual code,the data is not getting ordered.The keys in auditMap are 1,2,3,4 and in old values are 1,3,2 the result of old values should be 1,2,3 but its not happening

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

    If all you want is that, at the end of that code snippet, both auditMap and oldValues should be in same order, just run them through Collections.sort() method with appropriate comparator for the Field objects.

    If not, then create auditMapArray, a Field Array of size as that of oldValues (you certainly need to declare this outside IF block), after the IF block. Everytime you want to insert an element into auditMap, find its index in oldValues (assuming you have proper equals method implementation to check if two Field objects are equal) and insert at the same location in auditMapArray too, and finally get the auditMap list using Arrays.asList(auditMapArray).

    Modified version of your code snippet that ensures that all the values in oldValues that are present in auditMap are in the same order. Any additional elements into auditMap are appended at the end.

    Map<Long, String> attfieldMap = null;
    attfieldMap = view.getNameMap(form);
    
    List<Field> oldValues = new ArrayList<Field>();
    if (itemId != 0) {
    
        Item dom = getRecord();
    
        Map<FormField, Object> tempValues = dom.getValues();
    
        for (Map.Entry<FormField, Object> values : tempValues
                .entrySet()) {
            Field oldFld = new Field();
            Form form = (Form) values.getKey();
    
            if (attfieldMap.get(form.getField().getId()) != null) {
                oldFld.setKey(attfieldMap.get(form.getField()
                        .getId()));
                oldFld.setValue(values.getValue());
    
                oldValues.add(oldFld);
            }
        }
    }
    
    List<Field> otherFields = new ArrayList<Field>();
    Field [] auditMapArray = new Field[oldValue.size()];
    int index;
    for (Map.Entry<Long, String> attfieldEntry : attfieldMap.entrySet()) {
        // Mandates to declare within loop
        Field attributeFld = new Field;
    
        attributeFld.setKey(attfieldEntry.getValue());
        attributeFld.setValue(String.valueOf(attfieldEntry.getKey()));
    
        index = oldValues.indexOf(attributeFld);
        if (index > 0) {
            auditMapArray[index] = attributeFld;
        }
        else
        {
            System.err.println(attributeFld + " not found in oldValues");
            otherFields.add(attributeFld);
        }
    }
    
    List<Field> auditMap = Arrays.asList(auditMapArray);
    auditMap.addAll(otherFields);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists. They are sortable but not connected (elements of list one
I have two lists which are guaranteed to be the same length. I want
In my application, I have two lists of sounds from different categories and a
I have two Lists of dictionaries. Both are not not null. How to merge
I have two Lists, they look like this <List> ads [0] Headline = Sony
I have two lists. Ex: List<string> expected = new List<string>(); expected.Add( a ); expected.Add(
I have two lists: [a, b, c] [d, e, f] I want: [a, d,
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
i have two lists: List<comparerobj> list_c = new List<comparerobj>(); List<comparerobj> list_b = new List<comparerobj>();
I have two methods (in C#): List<Pizza> CookPizza(List<Order>); List<HappyCustomers> DeliverPizza(List<Pizza>); These operations have no

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.