Home > Enterprise >  Bubble sort or aphabetical order
Bubble sort or aphabetical order

Time:09-11

The whole idea of this code is to convert string numbers into the numbers then to sum it and sort it in specific order. So I Have a class called "Sort" that takes a string "TabName" and int "TabCV" and in this method I want to sort list of that class by int "TabCV" but if the int is in both cases similiar then I want to sort it in aphabetical order. The problem is that I am getting an error called "Cannot apply indexing with [] to an expression of type 'IOrderedEnumerable'"

class Sort
{
    public string TabName { get; set; }
    public int TabCV { get; set; }
}

static List<Sort> Sorts(List<Sort> list)//Retarted Bubble sort
{
    Sort temp = new Sort();
    var tempList = new List<Sort>();
    for (int j = 0; j <= list.Count - 2; j  )
    {
        for (int i = 0; i <= list.Count - 2; i  )
        {
            if (list[i].TabCV > list[i   1].TabCV)
            {
                temp = list[i   1];
                list[i   1] = list[i];
                list[i] = temp;
            }
            else if(list[i].TabCV == list[i   1].TabCV)
            {
                tempList.Add(list[i   1]);
                tempList.Add(list[i]);

                var tempTempList = tempList.OrderBy(x => x.TabName);

                list[i   1] = tempTempList[1]; <= Here I am getting an error
                list[i] = tempTempList[0]; <= Here I am getting an error

                tempList.Clear();
            }
        }
    }
    return list;
}

Before when I tried a little diffrent thing which looked like this It worked for first 3 exmaples then it was messed up again. (I think this approach is more retarded than what I am trying to do know but idk)

static List<Sort> Sorts(List<Sort> list)//Bubble sort retarted
{
    Sort temp = new Sort();
    var tempList = new List<Sort>();
    for (int j = 0; j <= list.Count - 2; j  )
    {
        for (int i = 0; i <= list.Count - 2; i  )
        {
            if (list[i].TabCV > list[i   1].TabCV)
            {
                temp = list[i   1];
                list[i   1] = list[i];
                list[i] = temp;
            }
            else if(list[i].TabCV == list[i   1].TabCV)
            {
                tempList.Add(list[i   1]);
                tempList.Add(list[i]);

                tempList.OrderBy(x => x.TabCV.ToString());

                list[i   1] = tempList[1];
                list[i] = tempList[0];

                tempList.Clear();
            }
        }
    }
    return list;
}

Example of test cases : "103 123 4444 99 2000", "2000 10003 1234000 44444444 9999 11 11 22 123"

CodePudding user response:

Skip the whole bubble sorting, that does the list for you, just implement your sort logic:

public class Sort : IComparable<Sort> {

    public string? TabName { get; set; }
    
    public int TabCV { get; set; }

    public int CompareTo(Sort? other) {
        if (other is null) return 1;
        if (this.TabCV < other.TabCV) return -1;
        if (this.TabCV > other.TabCV) return 1;
        return StringComparer.OrdinalIgnoreCase.Compare(this.TabName, other.TabName);
    }

}

and then call list.Sort(); after filling it.

  •  Tags:  
  • c#
  • Related