ddx Posted November 30, 2004 Posted November 30, 2004 Hello, I have this array: [3,1,2,2] [2,8,7,1] [3,3,9,6] [1,4,3,5] and i'd like to sort it in ascending order by the first element into: [1,4,3,5] [2,8,7,1] [3,1,2,2] [3,3,9,6] I tried Array.Sort by that only works for 1 dimensional arrays. How does one accomplish this in C#? Many Thanks. Quote
coldfusion244 Posted November 30, 2004 Posted November 30, 2004 i'd like to sort it in ascending order by the first element into: [1,4,3,5] [2,8,7,1] [3,1,2,2] [3,3,9,6] I tried Array.Sort by that only works for 1 dimensional arrays. How does one accomplish this in C#? Many Thanks. There are a few different methods, I believe this will help Sorting Methods. Just make sure when you use them instead of using array use array[0] so that you know to use the first integer in the ith array. Quote -Sean
Richard Crist Posted December 31, 2004 Posted December 31, 2004 The beauty of objects Hello, I have this array: [3,1,2,2] [2,8,7,1] [3,3,9,6] [1,4,3,5] and i'd like to sort it in ascending order by the first element into: [1,4,3,5] [2,8,7,1] [3,1,2,2] [3,3,9,6] I tried Array.Sort by that only works for 1 dimensional arrays. How does one accomplish this in C#? Many Thanks. You can use the .NET object reference standard to your advantage. As you add array elements of form [n,n,n,n...] you are creating objects which are just references. Using the methods from coldfusion244's website link, you can index into each array object then simply swap them via the reference ability. That is, in typical 3GL language style, you can create a "temporary" variable (object of type array [n,n,n,n...]) to use during the swapping process. You can index into each array member object [0][0], then [0][0], etc., and when you want to swap an array member you simply use object reference ability to do so. You set your temporary object of type [n,n,n,n...] equal to the first, set the second equal to the temp, then set the first equal to the second. Basically, you are just doing two things for each compare step: 1) Index into the two array object members to be compared 2) If they are to be swapped simply swap them via object (memory) reference Hope this makes sense. :) Quote nothing unreal exists .NET Framework Homepage ~ Visual C# Spec ~ C++/CLI Spec ~ Visual Basic .NET Spec
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.