0

I am trying to manually sort an array using string inputs that end with a number which I want to sort from highest to lowest.

For example I can start with this output: ,Name1: 1540 ,Name2: 2660 ,Name3: 80 ,Name4: 380

And in the end it should look like this: ,Name2: 2660 ,Name1: 1540 ,Name4: 380 ,Name3: 80

private string[] OrderHighToLow(string[] data)
    {
        string temp;
        for (int i = 0; i < data.Length; i++)
        {
            for (int y = 0; y < i; y++)
            {
                if (int.Parse(data[y].Substring((data[y].IndexOf(':') + 2))) > int.Parse(data[i].Substring((data[i].IndexOf(':') + 2)))) 
                {
                    temp = data[i];
                    data[i] = data[y];
                    data[y] = temp;
                }
            }
        }
        return data;
    }

This is what I have tested. According to me, this should work, but the point is it doesn't, the application just crashes. So, if anyone here can figure out why that may be, I would be very thankful. Thanks in advance.

13
  • 1
    Define "just crashes". Do you get a stack trace of some kind? Commented Mar 30, 2022 at 19:47
  • 1
    Why not do data.OrderBy(d=>-int.Parse(d)).ToArray() Commented Mar 30, 2022 at 19:49
  • System.ArgumentOutOfRangeExceptions: start index must not be greater than the length of the string Commented Mar 30, 2022 at 19:51
  • 1
    @LukasMannerstål: That means that some of the strings in your data doesn't look how you think it does. Put a breakpoint on the entry to your function and inspect that data. Most likely with the way you're parsing it, you're ending up with an empty string or something. Commented Mar 30, 2022 at 19:54
  • 2
    When I manually parse your inputs and pass them into your code, it's not throwing an exception: it's just sorting ascending rather than descending. I'm voting to close based on this being not reproducible. Commented Mar 30, 2022 at 19:56

2 Answers 2

1

your comparison is worng

int.Parse(data[y].Substring((data[y].IndexOf(':') + 2)))

less than Not greater than

 if (int.Parse(data[y].Substring((data[y].IndexOf(':') + 2))) < int.Parse(data[i].Substring((data[i].IndexOf(':') + 2)))) 
Sign up to request clarification or add additional context in comments.

Comments

1

This should do the job with the Help of System.Linq:

private string[] OrderHighToLow(string[] data)
{
    //create a temporary dictionary, which makes sorting easier
    var tmpMap = new Dictionary<string, int>();
    foreach (var item in data)
    {
        //split the string 
        var elems = item.Split(":");
        //add string and int pair to the dictionary
        tmpMap.Add(elems[0], int.Parse(elems[1]));
    }

    //sort Dictionary by value and use select to rebuild the string, then convert it back to an array
    return tmpMap.OrderByDescending(x => x.Value).Select(x => $"{x.Key}: {x.Value}").ToArray();
}

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.