-5

Here is example of how to sort array of objects by it's fields. I need to create function which will do same thing but WITHOUT Linq, Generics or any other classes.

p.s You can add methods in Test class to compare fields.

using System;
using System.Linq;

class Test {
    public int Count;
    public int Sum;
}

class Program {
    static void Main() {
        Test a1 = new Test() {
            Count = 1 ,
            Sum = 20
        };
        Test a2 = new Test() {
            Count = 2 ,
            Sum = 10
        };
        Test a3 = new Test() {
            Count = 3 ,
            Sum = 30
        };

        var arr = new Test[] { a1, a2, a3};

        var result = arr.OrderBy(n => n.Count).ToList();

        foreach (var item in result) {
            Console.WriteLine(item.Count);
        }
    }

    static void MyOrder() {
        //function which will sort passed array of objects by fields
    }
}
3

1 Answer 1

0

One method is to use Array.Sort() static method. But if you want to use it, you class must implement IComparable interface, for example:

class Test : IComparable
{
  public int Count;
  public int Sum;

  public int CompareTo(object obj)
  {
    if (!(obj is Test))
      throw new ArgumentException("You can't compare two objects of different types!");

    var test = (Test)obj;
    if (test.Count < this.Count) return 1;
    else if (test.Count > this.Count) return -1;
    else return 0;
  }
}

And then code would become:

var arr = new Test[] { a1, a3, a2 };
Array.Sort(arr);

EDIT:

If you want to change ordering field at runtime, you can use IComparer interface as folloing:

public class Test
{
  public int Count;
  public int Sum;
}

public class TestComparerBySum : IComparer<Test>
{
  public int Compare(Test x, Test y)
  {
    if (x.Sum > y.Sum) return 1;
    else if (x.Sum < y.Sum) return -1;
    else return 0;
  }
}

public class TestComparerByCount : IComparer<Test>
{
  public int Compare(Test x, Test y)
  {
    if (x.Count > y.Count) return 1;
    else if (x.Count < y.Count) return -1;
    else return 0;
  }
}

And use it in code like this:

var arr = new Test[] { a3, a2, a1 };

Array.Sort(arr, new TestComparerBySum());

Array.Sort(arr, new TestComparerByCount());
Sign up to request clarification or add additional context in comments.

8 Comments

What if i want to sort by Count ? or i need to use both of them?
@LukaMamulaishvili It compares by Count, take a look at CompareTo method! And try it, then you'll see that it compares just like you want.
sorry i mean sum. can i use both of them ?
@LukaMamulaishvili If you want to use Sum, then change all Count to Sum in CompareTo method. What do you mean by "can i use both of them?" ??
@LukaMamulaishvili Yes, your requirement was to not pass ordering field explicitly, so somehow you need to let the compiler know, what to compare by! And the only way is to write a code. So, yes, you need to change code everytime you change your mind, there's no way around.
|

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.