-1

Hello I would Like to receive some help. I have this structure:

 struct data
    {
        public String names;
        public int number;

    }

I've been asked to show this structure in the console sorted alphabetically (by evaluating the names) I don't really know how to do this, I know how to sort arrays but i don't know how to sort a structure like this.

I am a beginner, any help is received thanks.

2
  • Show how you sort your array. The only thing you need to do is to change array subscript to array subscript + property in your sorting condition. Commented Nov 7, 2016 at 4:02
  • nested for loop checking data[i].names with the temp variable or whatever you have? Commented Nov 7, 2016 at 4:02

1 Answer 1

2

this might do the trick for you

data[] datas = new[] { 
     new data() { names = "Mohit", number = 3 },
     //More data like that
}

and then

Array.Sort<data>(datas, (x,y) => x.names.CompareTo(y.names));
//or
Array.Sort(datas, (x,y) => string.Compare(x.names, y.names));

Or by using System.Linq

datas.OrderBy(x=>x.names);
Sign up to request clarification or add additional context in comments.

1 Comment

Felt happy to help you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.