-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.

1 Answers1

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);
Mohit S
  • 13,723
  • 6
  • 34
  • 69