Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sort string array using LINQ

Tags:

c#

asp.net

linq

I has a string array declare as below

string[][] data = new string[3][];
string[] name = new string[10];
string[] contact = new string[10];
string[] address = new string[10];

After i fill the data to name, address and contact, the address can be empty string in some data. After that I assign it to string array data.

data[0] = name;
data[1] = contact;
data[2] = address

How I can sort the string array by name using LINQ. I try data = data.orderby(y => y[0]).ToArray();

but this sort will change the sequence of the string array. Suppose data[0] is store name but after sorting it become store address.
Any one has idea how can I sort the record? Please help

like image 458
Bubble Bub Avatar asked Sep 23 '13 04:09

Bubble Bub


People also ask

How to use LINQ sort?

LINQ Sort is used to re-order or re-arrange the given collection of elements into ascending or descending order depends upon the attributes. The sorting operator arranges the sequence of items into ascending or descending order. Given below are the five LINQ sorting operators with their syntax as follows: 1. OrderBy 2. OrderByDescending

How to sort an array in ascending order?

1 Using the Arrays.sort () Method. In Java, Arrays is the class defined in the java.util package that provides sort () method to sort an array in ascending order. 2 Sort String Array in Ascending Order or Alphabetical Order. ... 3 Sort String Array in Descending Order or Reverse Natural Order 4 Using the reverseOrder () Method. ...

What is the use of Array sort in C?

Array is the static helper class that includes all utility methods for all types of array in C#. The Array.Sort () method is used to sort an array in different ways. The following example sorts an array in ascending order.

How to sort a one-dimensional array in Python?

We can sort a one-dimensional array in two ways, using Array.Sort () method and using LINQ query. Array is the static helper class that includes all utility methods for all types of array in C#. The Array.Sort () method is used to sort an array in different ways. The following example sorts an array in ascending order.


1 Answers

You can use this to sort the name array (which is stored at data[0]):

data[0] = data[0].OrderBy(x => x).ToArray();

However, this will cause the data stored in the other arrays to loose any meaningful correlation to the name array (e.g. name[3] most likely will not match up with contact[3]). To avoid this, I'd strongly recommend using a class to store this information:

class MyClass // TODO: come up with a better name
{
    public string Name { get; set; }
    public string Contact { get; set; }
    public string Address { get; set; }
}

To declare the array, use:

MyClass[] data = new MyClass[10];
data[0] = new MyClass   // Populate first record
{
    Name = "...",
    Contact = "...",
    Address = "...",
};

And to sort the array:

data = data.OrderBy(x => x.Name).ToArray();

Or this:

Array.Sort(data, (x, y) => x.Name.CompareTo(y.Name));

The second option is more efficient as it rearranges the elements in place, and doesn't require allocating a new array to store the results.

Or alternatively, use a List<T>:

List<MyClass> data = new List<MyClass>(10);
data.Add(new MyClass   // Populate first record
{
    Name = "...",
    Contact = "...",
    Address = "...",
});

And to sort the list:

data.Sort((x, y) => x.Name.CompareTo(y.Name));

This will have similar performance to the Array.Sort method, however, it is a much better option if you need to be able to add or remove elements from your list dynamically.

like image 187
p.s.w.g Avatar answered Sep 27 '22 17:09

p.s.w.g