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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:18:13+00:00 2026-05-24T16:18:13+00:00

I have a string array like this: string[] names = new string[] { john,

  • 0

I have a string array like this:

string[] names = new string[] { "john", "paul", "ringo", "george", "janis" };

I want to sort this array using a custom criteria. It can’t be alphabetical. It might be in this order: pgrj.

I’ve tried to implement a new IComparer, but inside the Compare() method I can’t use string.Compare cause it would sort alphabetically and I don’t want to do that.

The question is: How can I sort the names array in this order: pgrj ? In the ‘j’ case. Janis might be before John.

Thanks.

  • 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-24T16:18:13+00:00Added an answer on May 24, 2026 at 4:18 pm

    I would make an array of the letters of the alphabet, in the order that they should be sorted. Let’s call this array sort. You could then do something like

    for (int i = 0; i < s1.Length; i++)
    {
        int s1Value = sort.IndexOf(s1[i]);
        int s2Value = sort.IndexOf(s2[i]);
    
       if(s1Value > s2Value)
           return 1;
    
       if(s2Value > s1Value)
           return -1;
    }
    
    return 0;
    

    Now a few disclaimers.

    1. I might have my -1 and 1 mixed up. I haven’t looked at ICompare in a while and I forget which is which.
    2. This does not handle the two strings being different lengths.
    3. I have not tested this code.

    You could improve on this by making a custom class to assign a weight to each character.

    class CustomAlphabetSort
    {
         public char Character { get; set; }
         public int Weight { get; set; }
    }
    

    From here you can sum of the value for each letter of a string and do the comparison that way. It might be a little more complex than your situation requires

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

Sidebar

Related Questions

I have a multi-dim string array something like this:- string[,] names = new string[2,
Lets say I have an array like this: string [] Filelist = ... I
i have an string data array which contains data like this 5~kiran 2~ram 1~arun
I have string that I want to chop to array of substrings of given
If I have a plist which is structured like this: Root Array Item 0
Say I have an array like so in my code behind: public static string[]
If I have something like this from the server side, from a fetch: array(1)
serial.write() method in pyserial seems to only send string data. I have arrays like
I have a one-dimensional array of strings in JavaScript that I'd like to turn
I have a simple 2D array of strings and I would like to stuff

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.