Sorting {RESOLVED}

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 3932 Views - Last Post: 27 December 2009 - 04:02 AM Rate Topic: -----

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1637
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Sorting {RESOLVED}

Posted 20 December 2009 - 10:20 AM

Something I'm trying here, let's say I'm wanting to load the values in the ProcessPriorityClass enum into a ComboBox. I know I can use this to get the values into a generic list

List<ProcessPriorityClass> a = Enum.GetValues(typeof(ProcessPriorityClass)).Cast<ProcessPriorityClass>().ToList();



The enum has values like so

// Summary:
//	 Indicates the priority that the system associates with a process. This value,
//	 together with the priority value of each thread of the process, determines
//	 each thread's base priority level.
public enum ProcessPriorityClass
{
	// Summary:
	//	 Specifies that the process has no special scheduling needs.
	Normal = 32,
	//
	// Summary:
	//	 Specifies that the threads of this process run only when the system is idle,
	//	 such as a screen saver. The threads of the process are preempted by the threads
	//	 of any process running in a higher priority class.
	Idle = 64,
	//
	// Summary:
	//	 Specifies that the process performs time-critical tasks that must be executed
	//	 immediately, such as the Task List dialog, which must respond quickly when
	//	 called by the user, regardless of the load on the operating system. The threads
	//	 of the process preempt the threads of normal or idle priority class processes.
	High = 128,
	//
	// Summary:
	//	 Specifies that the process has the highest possible priority.
	RealTime = 256,
	//
	// Summary:
	//	 Specifies that the process has priority above System.Diagnostics.ProcessPriorityClass.Idle
	//	 but below System.Diagnostics.ProcessPriorityClass.Normal.
	BelowNormal = 16384,
	//
	// Summary:
	//	 Specifies that the process has priority above System.Diagnostics.ProcessPriorityClass.Normal
	//	 but below System.Diagnostics.ProcessPriorityClass.High.
	AboveNormal = 32768,
}



So it always sorts based on the value (So AboveNormal is the last item in the list) and I want them sorted alphabetically. Anyone got any tips on the best (efficient) way of sorting this (I guess sort ot on the int value in reverse)

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting {RESOLVED}

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Sorting {RESOLVED}

Posted 20 December 2009 - 10:28 AM

Store the string representation of the enum into the combobox and convert it back to an enum when you need it using Enum.Parse().
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1637
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Sorting {RESOLVED}

Posted 20 December 2009 - 11:12 AM

I think you're misunderstanding what I'm asking. I can populate the ComboBox, I can parse the selected value no problems. What I'm wanting to do is sort the values alphabetically in the ComboBox and List<T>.Sort() does not sort right (It sorts on the int value).

I get the values like so

public  List<T> ToGenericList<T>()
{
	Type t = typeof(T);

	List<T> list = new List<T>();

	if (!t.IsEnum)
		throw new ArgumentException("T must be a System.Enum type");

	foreach (T value in Enum.GetValues(typeof(T)))
		list.Add(value);

	list.Sort();
	return list;
}


Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Sorting {RESOLVED}

Posted 20 December 2009 - 11:25 AM

Maybe something like this?

 
class Program
{
	public enum ProcessPriorityEnum
	{
		Normal = 32,
		Idle = 64,
		High = 128,
		RealTime = 256,
		BelowNormal = 16384,
		AboveNormal = 32768
	}

	static void Main(string[] args)
	{
		var ordered = from e in Enum.GetValues(typeof(ProcessPriorityEnum)).Cast<ProcessPriorityEnum>().ToList()
					 orderby e.ToString()
					 select e;

		IList<ProcessPriorityEnum> list = ordered.ToList<ProcessPriorityEnum>();
		ProcessPriorityEnum singleEnum = list[2];
		int enumValue = (int)singleEnum;
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1637
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Sorting {RESOLVED}

Posted 20 December 2009 - 11:55 AM

@FlashM:

I changed my method to use your idea and they come in the same order

public List<T> ToGenericList<T>()
{
	Type t = typeof(T);
	List<T> list = new List<T>();

	if (!t.IsEnum)
		throw new ArgumentException("T must be a System.Enum type");

	var ordered = from e in Enum.GetValues(typeof(T)).Cast<T>().ToList()
				  orderby e.ToString()
				  select e;

	list = ordered.ToList<T>();

	return list;
}


Was This Post Helpful? 0
  • +
  • -

#6 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Sorting {RESOLVED}

Posted 20 December 2009 - 12:10 PM

I just tried your code and I get alphabetically ordered list returned...

List<ProcessPriorityEnum> lst = ToGenericList<ProcessPriorityEnum>();

This post has been edited by FlashM: 20 December 2009 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

#7 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1637
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Sorting {RESOLVED}

Posted 20 December 2009 - 12:16 PM

I dont get it, but I found a resolution

List<ProcessPriorityClass> list = SystemUtilities.Instance.ToGenericList<ProcessPriorityClass>();

foreach (ProcessPriorityClass s in list)
	cboGetPriority.Items.Add(s);

cboGetPriority.Sorted = true;


Was This Post Helpful? 0
  • +
  • -

#8 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Sorting {RESOLVED}

Posted 20 December 2009 - 12:23 PM

But it should definitely work:

List<ProcessPriorityEnum> bla = Enum.GetValues(typeof(ProcessPriorityEnum)).Cast<ProcessPriorityEnum>().ToList();
Console.WriteLine("Unsorted list output:");
foreach (ProcessPriorityEnum en in bla)
{
	Console.WriteLine(en.ToString());
}

List<ProcessPriorityEnum> lst = ToGenericList<ProcessPriorityEnum>();
Console.WriteLine("\r\nSorted list output:");
foreach (ProcessPriorityEnum en in lst)
{
	Console.WriteLine(en.ToString());
}



Here is my console output:
Unsorted list output:
Normal
Idle
High
RealTime
BelowNormal
AboveNormal

Sorted list output:
AboveNormal
BelowNormal
High
Idle
Normal
RealTime

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1637
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Sorting {RESOLVED}

Posted 20 December 2009 - 12:25 PM

Yeah, I was trying to use cboGetPriority.DataSource = list; and it wasn't sorting the values, but when I add then using a loop (like shown above) then they're sorted.
Was This Post Helpful? 0
  • +
  • -

#10 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Sorting {RESOLVED}

Posted 21 December 2009 - 07:47 AM

Another very clean approach :-)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace Processes
{
	public enum ProcessPriorityEnum
	{
		Normal = 32,
		Idle = 64,
		High = 128,
		RealTime = 256,
		BelowNormal = 16384,
		AboveNormal = 32768
	}

	public partial class Form1 : Form
	{
		private BindingList<ProcessPriorityEnum> bindingList = null;

		public Form1()
		{
			InitializeComponent();
			IEnumerable<ProcessPriorityEnum> ordered = (Enum.GetValues(typeof(ProcessPriorityEnum))
													   .Cast<ProcessPriorityEnum>().ToArray())
													   .OrderBy(p => p.ToString());

			bindingList = new BindingList<ProcessPriorityEnum>(ordered.ToList());
			cboPriorities.DataSource = bindingList;
		}

		private void cboPriorities_SelectedIndexChanged(object sender, EventArgs e)
		{
			ComboBox cb = null;
			if ((cb = sender as ComboBox) != null)
			{
				ProcessPriorityEnum current = (ProcessPriorityEnum)cb.SelectedItem;
				MessageBox.Show(((int)current).ToString());
			}
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2239
  • View blog
  • Posts: 9,409
  • Joined: 29-May 08

Re: Sorting {RESOLVED}

Posted 21 December 2009 - 03:50 PM

Have you also consider implementing an IComparer Interface to the ProcessPriorityClass. If you did you could tell the sort how to sort ProcessPriorityClasses.
See: Custom Sorting (it is VB.net though)
Was This Post Helpful? 0
  • +
  • -

#12 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 84
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Sorting {RESOLVED}

Posted 21 December 2009 - 06:34 PM

It's an enum - System.Diagnostics.ProcessPriorityClass.

I was going to have a go with c# 4.0, and IEnumerable.Zip to do it. Just for the practice. But first I've got to install VS2010...


Edit: here is is, purely OOI.

comboBox1.DataSource = Enumerable.Zip(Enum.GetNames(typeof(ProcessPriorityClass)).Cast<string>(), 
									  Enum.GetValues(typeof(ProcessPriorityClass)).Cast<ProcessPriorityClass>(), 
									  (name, value) => new { name, value }).OrderBy(pair => pair.name).ToList();
comboBox1.ValueMember = "value";
comboBox1.DisplayMember = "name";

This post has been edited by crepitus: 22 December 2009 - 03:30 AM

Was This Post Helpful? 0
  • +
  • -

#13 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Sorting {RESOLVED}

Posted 22 December 2009 - 01:26 AM

Implementing IComparer interface certainly is a solution but don't think it's really needed to do that for such a simple sorting.
Was This Post Helpful? 0
  • +
  • -

#14 fixo  Icon User is offline

  • D.I.C Regular

Reputation: 85
  • View blog
  • Posts: 335
  • Joined: 10-May 09

Re: Sorting {RESOLVED}

Posted 24 December 2009 - 03:11 PM

View PostAdamSpeight2008, on 21 Dec, 2009 - 02:50 PM, said:

Have you also consider implementing an IComparer Interface to the ProcessPriorityClass. If you did you could tell the sort how to sort ProcessPriorityClasses.
See: Custom Sorting (it is VB.net though)

Sorry Adam,
This example
Custom Sorting
it's not working for me at all (VS2005,Win XP)
I wrote the following code instead
(0.0001 is dummy fuzz factor value):

			Array.Sort(a,delegate(int x, int y) { return string.Compare((x * 0.0001).ToString(),(y * 0.0001).ToString()); });
			for (int i = 0; i < arr.Length; i++)
				this.listBox1.Items.Add(arr[i]);


Merry Christmas and Happy New Year

~'J'~
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2239
  • View blog
  • Posts: 9,409
  • Joined: 29-May 08

Re: Sorting {RESOLVED}

Posted 24 December 2009 - 03:33 PM

Why missing around with the delegates? Array.Sort as other overloads, the simplest for the IComparer is the following.
	Sub Main()
		Dim XS As Integer() = {1, 2, 12, 13, 14, 15, 16, 17, 18, 19, 3, 4, 5, 6, 7, 8, 9, 10, 11, 20}
		Dim Fs As New NumericComparer(False) ' Sorted Numerically
		Dim Ts As New NumericComparer(True) ' Sorted Textually
		Array.Sort(XS, Ts)
	End Sub


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2