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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:30:23+00:00 2026-06-05T19:30:23+00:00

I have a Map and a List . The List should be sorted based

  • 0

I have a Map and a List. The List should be sorted based on the Map key values. E.g:

Map = (<2,"Andy">,<4,"Karl">)
List = ("Kathy","Andy","Yiri","Jun","Karl")

I have to sort the List in such a way that at index : (2 : Andy) should be there
and at index (4 : Karl) should be there. For rest of elements order does not matter.
Rest of Conditions are :

  1. Entries in Map may or may not present in List ;(In this we can keep the order same)
  2. List could be empty
  3. Map could be empty

I am able to do this in 2 loops , I was curious to know if it is possible to achieve this in a single Loop.

  • 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-05T19:30:25+00:00Added an answer on June 5, 2026 at 7:30 pm

    You can use 1 loop to loop through all keys in the map, and search for the object in the List with indexOf(Object), then use set twice to swap the object to the correct position.

    Pseudocode (that looks like Java):

    Set<Map.Entry<Integer, String>> entrySet = map.entrySet();
    for (Map.Entry<Integer, String> e: entrySet) {
        int index;
        if ((index = list.indexOf(e.getValue())) >= 0 && index != e.getKey()) {
            list.set(index, list.set(e.getKey(), e.getValue()));
        }
    }
    

    Well, complexity is not that good: O(nk) where n is the number of elements in the List and k is the number of elements in the Map.

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

Sidebar

Related Questions

How should I map multiple columns with List in IBATIS ? I have a
I have a Map of Lists with key = GROUP and value = List
I have a map of the USA and a list of long, lat that
I have two activities in my Android project: Google Map Activity List Activity I'm
I have a map<string, list<int> > which I want to iterate over the list
I have a Map<Integer, List<Float>> that represent a position with some scores, and I
I have a map declared as follows: map < string , list < string
I have an unordered map: class O(val a: Int) Map[String, List[O]] which I'd like
I have a List of keys say 'ListA'. And a map of keys &
I have a Scheme macro and a long list, and I'd like to map

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.