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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:08:04+00:00 2026-05-11T17:08:04+00:00

I would like to sort an array of strings in a directory, given a

  • 0

I would like to sort an array of strings in a directory, given a custom mapping (it’s actually a sorting of stock names based on their sector). I am unsure of what data structures to use to represent the mapping, and how to write the custom sort method.

So for instance, suppose I had the following string array:

string[] fileNames = "bac.csv", "c.csv", "cvx.csv", "java.csv", "msft.csv", "xom.csv";

And here are the mappings:

{"bac", "c"} => 0
{"msft", "java"} => 1
{"xom", "cvx"} => 2

I would like string[] customSort(string[] fileNames) to return the following:

"bac.csv", "c.csv", "java.csv", "msft.csv", "xom.csv", "cvx.csv"

What data structure would you use to represent the mappings, and what’s an elegant way of writing the sort method?

  • 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-11T17:08:04+00:00Added an answer on May 11, 2026 at 5:08 pm

    Array.Sort allows you to specify an array of keys, so you can do something like…

    int[] keys = new int[fileNames.Length];
    
    Dictionary<string, int> mapping = new Dictionary<string, int>(StringComparer.CurrentCultureIngoreCase);
    
    // set up our mappings like so
    mapping.Add("bac", 0);
    mapping.Add("c", 0);
    mapping.Add("msft", 1);
    mapping.Add("java", 1);
    mapping.Add("xom", 2);
    mapping.Add("cvx", 2);
    // etc
    
    for(int i=0; i < keys.Length; i++)
    {
        string token = System.IO.Path. GetFileNameWithoutExtension(fileNames[i]);
    
        int mappingKey;
    
        if(!mapping.TryGetValue(token, out mappingKey)) mappingKey = int.MaxValue;
    
        keys[i] = mappingKey; 
    }
    
    Array.Sort<int, string>(keys, fileNames);
    

    Just modify the keys[i] = -1; statement to get the proper value from your mappings given the token variable.

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

Sidebar

Related Questions

I have an array of strings (names) and i would like to sort them
Given an array 'a' I would like to sort the array by columns sort(a,
I would like to sort an array of strings (in JavaScript) such that groups
I would like to sort an array in ascending order using C/C++ . The
I have an array that I would like to sort using a date field
I would like to sort a list of strings which represent paths. The sort
I have an array named 'names' with strings looking like this: [name_23_something, name_25_something, name_2_something];
I have a UITableView containing names that I would like to group (and sort)
I have an array of items and I would like to sort on one
How would I go about sorting an array of NSString Objects by a custom

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.