Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

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

-Sean
  • 1 month later...
Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...