0

I am sending a ArrayList as below from a web service call :

private ArrayList testList = new ArrayList();

Which will store values like :

"xyz (pound) (4545)"
"abc (in)    (346)"
"def (off)   (42424)"

I use this because of two reasons :

1 : I have to fetch this value in ASP.NET 1.1 framework.

2 : I use testList.Sort(); before sending.

But now I want to send these values as :

"xyz"   "pound"  "4545"
"abc"  "in"    "346"
"def"   "off" "42424"

So I found a way as below :

string[][] data = { new string[]{"xyz", "pound", "4545"},
                    new string[]{"abc", "in", "346"}, 
                    new string[]{"def", "off", "42424"}};

Question is : How can I sort it effectively?? OR Is there a better way to address this ?

Sort will be done based on first element :

abc
def
xyz

3 Answers 3

5

You write that you have to read this value in ASP 1.1, so I assume that you have a more modern version of the .NET framework available on the sending side.

If that is the case, you can use the OrderBy method of LINQ, included in Framework 3.5 or higher:

string[][] data = { new string[] { "xyz", "pound", "4545" }, 
                    new string[] { "abc", "in", "346" }, 
                    new string[] { "def", "off", "42424" } };
data = data.OrderBy(entry => entry[0]).ToArray();  // sorts by first field
Sign up to request clarification or add additional context in comments.

4 Comments

Just wonder how this solution differs from my? Also comment it is wrong, because sorting is done by array item, not by field
Off the question -- I am adding these string arrays in loop...like this //Adding each information to the array of string. data = new string[3]; data[0] = "xyz"; data[1] = "8787"; data[2] = "dad"; //Adding each row to the array list. arrList.Add(data);//Adding array list to the property object. vinDescription.ShowOptionalEquipment = (string[][])arrList.ToArray(typeof(string[])); ... Is this correct or a better way is there??
@lazyberezovsky: It doesn't differ significantly, we just happened to come up with the same solution at the same time. I used the term field in an informal sense here (call it an analogy or metaphor), since the inner array seems to represent some kind of record.
@Learner: I don't see a problem with your solution.
2

Just sort inner arrays by first item in array

string[][] sorted = data.OrderBy(array => array[0]).ToArray();

2 Comments

What will be the type of sorted ??
Array of string arrays, or string[][]. I used var just for shortening
0

Accepted answer is correct only in case we are trying to compare first letter of the string. I had similiar issue and to get accurate sorting, you need to order by whole string not just first char.

data = data.OrderBy(entry => entry).ToArray(); // sorts by first field

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.