Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a listview which looks like this:

 

COL1 | COL2

1 | personA

2 | personB

3 | personC

 

I also have an up and down button so the user can click on a person and move him up or down the list like below.

 

COL1 | COL2

1 | personB

2 | personC

3 | personA

 

 

I have read a few places about sorting ascending or descending, but this is a little different.

 

Can I take listview item 0 and place it in position 2 or wherever it needs to go.

 

I could always take the contents out to an array and then then move the items around then put them back in the listview.

 

Just checking to see if I have missed a simple way.

 

Thanks

Posted

You don't have to take them out and put them in an array. Look into Bubble Sorting, you can make your own sorting code the way you like it.

 

Here is what I use sometimes: It is confusing but works. And your code does not need to be anything near so confusing.

public static StringBuilder SortErrB=new StringBuilder("Errors in a.Strings.CompNbrs:\n\n");
		/// <summary>
		/// Sorts string arrays!! Has a string builder called SortErrB to show all errors to user.
		/// </summary>
		public static string[] Sort(string[] x, char AorD)
		{
			if(x.Length==1)  return x;
			string[]Pths=new string[x.Length];
			Array.Copy(x, Pths, x.Length);
			double[]D=new double[2];
			
			if(AorD=='A')
			{
				for(int i=0; i < Pths.Length; i++)
				{
					for(int j=0; j < Pths.Length-1; j++)
					{
						int shortt = 0;  int longg = 0;
						if(Pths[j].Length < Pths[j+1].Length) {shortt= 0; longg= 1;}
						else if(Pths[j].Length > Pths[j+1].Length) {shortt= 1; longg= 0;}

						if(Pths[j]==Pths[j+1]) {}
						else if(Pths[j+longg].StartsWith(Pths[j+shortt]) && shortt != longg)
						{
							int last= Pths[j+shortt].Length-1;
							int lastAfter= last+1;
							
							if((Char.IsDigit(Pths[j+shortt][last]) || Pths[j+shortt][last]=='-' || Pths[j+shortt][last]=='.') &&
								(Char.IsDigit(Pths[j+longg][last]) || Pths[j+longg][last]=='-' || Pths[j+longg][last]=='.') &&
								(Char.IsDigit(Pths[j+longg][lastAfter]) || Pths[j+longg][lastAfter]=='-' || Pths[j+longg][lastAfter]=='.'))
							{
								if(longg > shortt)
									D= CompNbrs(ref Pths[j+shortt],ref Pths[j+longg],
										GetIndex(ref Pths[j+shortt],ref Pths[j+longg], last));
								else D= CompNbrs(ref Pths[j+longg], ref Pths[j+shortt],
										 GetIndex(ref Pths[j+longg], ref Pths[j+shortt], last));
								if(D[0] > D[1]) Swap(Pths, ref j);
							}
						}
						else
						{
							for(int k=0; k<Pths[j].Length && k<Pths[j+1].Length; k++)
							{
								if((Char.IsDigit(Pths[j][k]) || Pths[j][k]=='-' || Pths[j][k]=='.') &&
									(Char.IsDigit(Pths[j+1][k]) || Pths[j+1][k]=='-' || Pths[j+1][k]=='.'))
								{
									D= CompNbrs(ref Pths[j],ref Pths[j+1], k);
									if(D[0] > D[1]) {Swap(Pths, ref j);  k= Pths[j].Length;}
									else if(D[0] < D[1]) k= Pths[j].Length;
								}
								else if(Pths[j][k] > Pths[j+1][k])
								{
									Swap(Pths, ref j);
									k= Pths[j].Length;
								}
								else if(Pths[j][k] < Pths[j+1][k])
									k= Pths[j].Length;
							}
						}
					}
				}
			}
			else if(AorD=='D')
			{
				for(int i=0; i < Pths.Length; i++)
				{
					for(int j=0; j < Pths.Length-1; j++)
					{
						int shortt = 0;  int longg = 0;
						if(Pths[j].Length < Pths[j+1].Length) {shortt= 0; longg= 1;}
						else if(Pths[j].Length > Pths[j+1].Length) {shortt= 1; longg= 0;}

						if(Pths[j]==Pths[j+1]) {}
						else if(Pths[j+longg].StartsWith(Pths[j+shortt]) && shortt != longg)
						{
							int last= Pths[j+shortt].Length-1;
							int lastAfter= last+1;
							
							if((Char.IsDigit(Pths[j+shortt][last]) || Pths[j+shortt][last]=='-' || Pths[j+shortt][last]=='.') &&
								(Char.IsDigit(Pths[j+longg][last]) || Pths[j+longg][last]=='-' || Pths[j+longg][last]=='.') &&
								(Char.IsDigit(Pths[j+longg][lastAfter]) || Pths[j+longg][lastAfter]=='-' || Pths[j+longg][lastAfter]=='.'))
							{
								if(longg > shortt)
									D= CompNbrs(ref Pths[j+shortt],ref Pths[j+longg],
										GetIndex(ref Pths[j+shortt],ref Pths[j+longg], last));
								else D= CompNbrs(ref Pths[j+longg], ref Pths[j+shortt],
										 GetIndex(ref Pths[j+longg], ref Pths[j+shortt], last));
								if(D[0] < D[1]) Swap(Pths, ref j);
							}
						}
						else
						{
							for(int k=0; k<Pths[j].Length && k<Pths[j+1].Length; k++)
							{
								if((Char.IsDigit(Pths[j][k]) || Pths[j][k]=='-' || Pths[j][k]=='.') &&
									(Char.IsDigit(Pths[j+1][k]) || Pths[j+1][k]=='-' || Pths[j+1][k]=='.'))
								{
									D= CompNbrs(ref Pths[j],ref Pths[j+1], k);
									if(D[0] < D[1]) {Swap(Pths, ref j);  k= Pths[j].Length;}
									else if(D[0] > D[1]) k= Pths[j].Length;
								}
								else if(Pths[j][k] < Pths[j+1][k])
								{
									Swap(Pths, ref j);
									k= Pths[j].Length;
								}
								else if(Pths[j][k] > Pths[j+1][k])
									k= Pths[j].Length;
							}
						}
					}
				}
			}
			return Pths;
		}

C#
Posted

Forgot the most important part. The Bubble Sort itself:

/// <summary>
	/// Helper method for Sorting.
	/// </summary>
	static void Swap(string[]Arr, ref int firstPos)
	{
		string hold;
		hold= Arr[firstPos+1];
		Arr[firstPos+1]= Arr[firstPos];
		Arr[firstPos]= hold;
	}
	//--------------
	static void Swap(double[]Arr, ref int firstPos)
	{
		double hold;
		hold= Arr[firstPos+1];
		Arr[firstPos+1]= Arr[firstPos];
		Arr[firstPos]= hold;
	}

Notice that I did use a string array. But you don't have to. You can deal with the listbox directly. I use this because it is universal.

C#
  • *Experts*
Posted

Look into the ListView's Sort() method and its ListViewItemSorter

property.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

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