0

Right so i have a class I'm using to store a set of values

public class dataSet
 {
  public int Number;
  public double Decimal;
  public string Text;
  //etc...
 }

Then I've made an array of type dataSet

public static dataSet[] dataOne = new dataSet[100];

And i'm trying to sort the array of dataOne relevant to the values stored in the int Number stored within dataSet. I have a sort algorithm ready but i'm struggling to pass in the values stored solely in dataOne.Number so it just ends up being an integer array that i'm passing to the sort. I'm a total noob at programming so any help would be greatly appreciated.

Edit:

I need to call my sort function by passing it in the array of dataOne.Number if this is possible? So it's basically just passing the sort function an int[]

5
  • I would recommend looking at my answer again before proceeding with your accepted answer. Better to use standard sort methods if you can and defining your object as IComparable is in my opinion the best way to achieve this. Commented Apr 24, 2017 at 17:48
  • Yeah, I appreciate your answer it's just I'm limited with not being able to use inbuilt sorting methods in this particular instance. Else i'm sure yours would have been perfect for doing what i was asking. Thanks. Commented Apr 24, 2017 at 17:52
  • How is it you can't use Array.Sort? It is within the System namespace, which you're surely using. Unless it is for school or an interview? Commented Apr 24, 2017 at 17:53
  • It's for school haha. I am aware of it's capabilities otherwise. Commented Apr 24, 2017 at 18:01
  • Then implementing IComparable would probably look sketchy to the professor anyways. A little bit too advanced I would guess if you're working on writing your own sorting methods. Commented Apr 24, 2017 at 18:05

3 Answers 3

3

Give you already have data into your array named dataOne, you could try:

Linq Solution

Use linq to sort it, try this:

dataOne = dataOne.OrderBy(x => x.Number).ToArray();

Remember to add the namespace System.Linq to have access into these methods.

OrderBy allows you to pass an expression to sort data and it will return an IOrderedEnumerable. The ToArray will convert it to an array.

Not Linq Solution

If you are not allowed to use Linq. You could implement an class that implements IComparer<T> and implement the method Compare which takes two generics arguments. Use an instance of this comparer type to sort your data.

For sample, since you have your dataSet type defined, you could implement the comparer:

public class DataSetComparer : IComparer<dataSet>
{
    public int Compare(dataSet x, dataSet y)
    {
        // define the logic to sort here...
        return x.Number.CompareTo(y.Number);
    }
}

And then, use the comparer on the Array.Sort method:

Array.Sort(dataSet, new NumberComparer());

It will order your dataSets.

Sign up to request clarification or add additional context in comments.

5 Comments

I think you should comment using Linq to your answer
Hi, thanks for the quick response. Forgot to mention i'm limited by not using inbuilt sorting functions.
@publicMan If you need the answer relative to the sort algorithm code you've already written, you need to supply that code for us to see as part of your question.
Yeah, Linq is off limits for me unfortunately. Just wanting to know how to pass the dataOne.Number array to my sort function. It needs to be passed into the sort function as an integer array.
I've updated my answer to a non-linq solution as well. It is not handy, but it works. Take a look.
0

I'm not sure I follow why you can't use Linq. But that forces you do to something like this:

var numberValues = new List<int>();
foreach(var dataItem in dataOne)
{
    numberValues.Add(dataItem.Number);
}

Then you could pass numberValues.ToArray() to your sort method.

With Linq it would just be

dataOne.Select(d => d.Number).ToArray()

2 Comments

Will give this a try thanks, seems like it could work.
Hi, thanks this has helped best for my simple mind. Not trying to do anything too complicated here haha.
0

You should have dataset implement IComparable that way you can easily just do...

dataOne = dataOne.OrderBy(x => x).ToArray();

OR...

Array.Sort(dataOne);

Here is how to implement IComparable...

public class dataSet : IComparable
{
    public int Number;
    public double Decimal;
    public string Text;

    public int CompareTo(object obj)
    {
        if (obj == null)
            return 1;
        dataSet other = obj as dataSet;
        if (other != null) 
            return this.Number.CompareTo(other.Number);
        else
            throw new ArgumentException("Object is not a dataSet");
    }
}

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.