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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:24:20+00:00 2026-05-20T23:24:20+00:00

I have an ArrayList of strings that look as below, I would like to

  • 0

I have an ArrayList of strings that look as below, I would like to output a new ArrayList sorted in a particular way. But not sure of a good way of sorting it. Help will be really appreciated!

Original (can be in any random order):

1:1
0:0
0:1
2:1
1:0
2:0

Output:

2:0
2:1
1:0
1:1
0:0
0:1
  • 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-20T23:24:20+00:00Added an answer on May 20, 2026 at 11:24 pm

    While I think both of the other answers are spot-on, I’m going to assume you’re unfamiliar with some of the features of .NET 2.0 and .NET 3.5 they used. Let’s take it one step at a time.

    So you’re given an ArrayList holding the following data:

    { "1:1", "0:0", "0:1", "2:1", "1:0", "2:0" }
    

    First of all, nothing is wrong with using RegEx; perhaps a minor performance penalty. If the strings are really as simple as this, you can just use Split:

    string[] s = myArray[i].Split(new[] { ':' });
    int val1 = int.Parse(s[0]);
    int val2 = int.Parse(s[1]);
    

    However, since you said you’re using .NET 4, you really shouldn’t be using ArrayList at all — note that it requires you to cast your values to their appropriate type, e.g. string mystring = myArray[i] as string.

    There are plenty of great features you’re not taking advantage of, such as generics (in the .NET Framework since 2.0). Let’s write a function that is given an ArrayList, but returns a sorted generic List<string> (a list that only holds strings). Let’s have a look:

    /// <summary>
    /// This method takes in an ArrayList of unsorted numbers in the format: a:b
    /// and returns a sorted List<string> with a descending, b ascending
    /// <summary>
    public List<string> SortMyValues(ArrayList unsorted)
    {
        // Declare an empty, generic List of type 'TwoNumbers'
        List<MyTuple> values = new List<MyTuple>();
        foreach (object item in unsorted)
        {
            char[] splitChar = new char[] { ':' };
            string itemString = item as string;
            string[] s = itemString.Split(splitChar);
            values.Add(new MyTuple{
                FirstNumber = int.Parse(s[0]),
                SecondNumber = int.Parse(s[1])
            });
        }
        // Sort the values
        values.Sort();
        // Return a list of strings, in the format given
        List<string> sorted = new List<string>();
        foreach (MyTuple item in values)
        {
            sorted.Add(item.FirstNumber + ":" + item.SecondNumber);
        }
        return sorted;
    }
    
    public class MyTuple : IComparable {
        public int FirstNumber { get; set; }
        public int SecondNumber { get; set; }
    
        public int CompareTo(object obj)
        {
            if (obj is MyTuple)
            {
                MyTuple other = (MyTuple)obj;
    
                // First number descending
                if (FirstNumber != other.FirstNumber)
                return other.FirstNumber.CompareTo(FirstNumber);
                // Second number ascending
            return SecondNumber.CompareTo(other.SecondNumber);
            }
            throw new ArgumentException("object is not a MyTuple");
        }
    }
    

    Now, the above code works, but is really long. Note that you have to create a class just to hold these two values, make that class implement IComparable, etc, etc. Pretty annoying!

    .NET 3.5 came out with some great features, including anonymous types and LINQ. Let’s change our code to use both of those features.

    /// <summary>
    /// This method takes in an ArrayList of unsorted numbers in the format: a:b
    /// and returns a sorted List<string> with a descending, b ascending
    /// <summary>
    public List<string> SortMyValues(ArrayList unsorted)
    {
        // First, cast every single element of the given ArrayList to a string
        // The Cast<T> method will do this, and return an enumerable collection
        return unsorted.Cast<string>()
            // Now, let's take this string data and create our objects that will hold two numbers
            .Select(item => {
                // This is the body of an anonymous method with one parameter, which I called 'item'
                // This anonymous method will be executed for every element in the collection
                string[] s = item.Split(new[] { ':' });
                // Here we create a new anonymous type holding our numbers
                // No need to define a new dummy class!
                return new {
                    FirstNumber = int.Parse(s[0]),
                    SecondNumber = int.Parse(s[1])
                };
            })
            // Now that we've got our objects, let's sort them
            .OrderByDescending(x => x.FirstNumber)
            .ThenBy(x => x.SecondNumber)
            // Finally, now that we're sorted properly, let's format our string list output
            .Select(x => x.FirstNumber + ":" + x.SecondNumber)
            .ToList();
    }
    

    Our entire function is just one line now, and most of the code is comments. I encourage you to learn about and start using some of these features; it’ll make your code a lot easier to read and write 😉

    Hope this helped!

    Edit: In resopnse to your comment:

    What would it take to have them in the following order: 2:0 1:0 0:0 2:1 1:1 0:1

    It looks like you’re sorting by the second number, ascending, and then by the first number, descending.

    Simply change the code above to use:

    .OrderBy(x => x.SecondNumber)
    .ThenByDescending(x => x.FirstNumber)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an ArrayList that has lines of data that could look like:
I have an ArrayList<String> that I'd like to return a copy of. ArrayList has
Let's say I have two same strings inside an ArrayList... is there a way
I have a GWT/GAE project that has an arraylist of Strings on the server
I have an ArrayList<String> , and I want to remove repeated strings from it.
I have an arraylist that contains items called Room. Each Room has a roomtype
In Java, say you have a class that wraps an ArrayList (or any collection)
I have an arraylist that holds a subset of names found in my database.
I have a new web app that is packaged as a WAR as part
I have ArrayList of Strings. I need to randomize it by hash number. Example:

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.