ArrayList.Sort not sorting properly

D

djp

Hi
I have to sort arraylist. I tried to do this using this page as a reference:
http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm

I did it exactly the same way but unfortunately in opposite to the example
from this link here it does not sort.
I Create Nodes ( color, name ) and add them to nodeList in Graph class. Then
i try to sort..
The code is relatively simple:

namespace NaszaPierwszaListaSasiedztwa

{

class Program

{

public class Graph

{

class CompInv : IComparer

{

// Implement the IComparable interface.

public int Compare(object obj1, object obj2)

{


Node a, b;

a = (Node)obj1;

b = (Node)obj2;

int result = a.nodeList.Count.CompareTo(b.nodeList.Count);

Console.WriteLine(a.nodeList.Count.ToString() + " " +
b.nodeList.Count.ToString() + " " + result.ToString());

return result;

}

}


public ArrayList nodeList = new ArrayList();

public void Color()

{

CompInv comp = new CompInv();

nodeList.Sort(comp);

foreach (Node var in nodeList)

{

Console.WriteLine(var.Name.ToString());

}

}

private ArrayList segregator = new ArrayList();


}

public class Node

{

public ArrayList nodeList = new ArrayList();

public Node(int Color, int Name)

{

this.Color = Color;

this.Name = Name;

}

int Color = -1;

public int Name = -1;

}

/// <summary>

/// Main

/// </summary>

/// <param name="args"></param>

static void Main(string[] args)

{

Graph myGraph = new Graph();

Node node1 = new Node(-1, 1);

Node node2 = new Node(-1, 2);

Node node3 = new Node(-1, 3);

Node node4 = new Node(-1, 4);

Node node5 = new Node(-1, 5);



myGraph.nodeList.Add(node1);

myGraph.nodeList.Add(node4);

myGraph.nodeList.Add(node3);

myGraph.nodeList.Add(node5);

myGraph.nodeList.Add(node2);


// 1 - ( 5, 2)

((Node)myGraph.nodeList[0]).nodeList.Add(node5);

((Node)myGraph.nodeList[0]).nodeList.Add(node2);

// 5 - ( 1 )

((Node)myGraph.nodeList[4]).nodeList.Add(node1);

// 2 - ( 1, 3, 4 )

((Node)myGraph.nodeList[1]).nodeList.Add(node1);

((Node)myGraph.nodeList[1]).nodeList.Add(node3);

((Node)myGraph.nodeList[1]).nodeList.Add(node4);



// 3 - ( 2, 4 )

((Node)myGraph.nodeList[2]).nodeList.Add(node2);

((Node)myGraph.nodeList[2]).nodeList.Add(node4);



// 4 - ( 2, 3 )

((Node)myGraph.nodeList[3]).nodeList.Add(node2);

((Node)myGraph.nodeList[3]).nodeList.Add(node3);

myGraph.Color();

}


}

}





Thanks for any help

PK
 
N

Nicholas Paldino [.NET/C# MVP]

This isn't going to work. You are only comparing the count of the items
in the list that the nodes belong to, and returning that. You need to look
at the fields on a and b that you pass in, and then return a value based on
the fields on a and b.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

djp said:
Hi
I have to sort arraylist. I tried to do this using this page as a
reference:
http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm

I did it exactly the same way but unfortunately in opposite to the example
from this link here it does not sort.
I Create Nodes ( color, name ) and add them to nodeList in Graph class.
Then i try to sort..
The code is relatively simple:

namespace NaszaPierwszaListaSasiedztwa

{

class Program

{

public class Graph

{

class CompInv : IComparer

{

// Implement the IComparable interface.

public int Compare(object obj1, object obj2)

{


Node a, b;

a = (Node)obj1;

b = (Node)obj2;

int result = a.nodeList.Count.CompareTo(b.nodeList.Count);

Console.WriteLine(a.nodeList.Count.ToString() + " " +
b.nodeList.Count.ToString() + " " + result.ToString());

return result;

}

}


public ArrayList nodeList = new ArrayList();

public void Color()

{

CompInv comp = new CompInv();

nodeList.Sort(comp);

foreach (Node var in nodeList)

{

Console.WriteLine(var.Name.ToString());

}

}

private ArrayList segregator = new ArrayList();


}

public class Node

{

public ArrayList nodeList = new ArrayList();

public Node(int Color, int Name)

{

this.Color = Color;

this.Name = Name;

}

int Color = -1;

public int Name = -1;

}

/// <summary>

/// Main

/// </summary>

/// <param name="args"></param>

static void Main(string[] args)

{

Graph myGraph = new Graph();

Node node1 = new Node(-1, 1);

Node node2 = new Node(-1, 2);

Node node3 = new Node(-1, 3);

Node node4 = new Node(-1, 4);

Node node5 = new Node(-1, 5);



myGraph.nodeList.Add(node1);

myGraph.nodeList.Add(node4);

myGraph.nodeList.Add(node3);

myGraph.nodeList.Add(node5);

myGraph.nodeList.Add(node2);


// 1 - ( 5, 2)

((Node)myGraph.nodeList[0]).nodeList.Add(node5);

((Node)myGraph.nodeList[0]).nodeList.Add(node2);

// 5 - ( 1 )

((Node)myGraph.nodeList[4]).nodeList.Add(node1);

// 2 - ( 1, 3, 4 )

((Node)myGraph.nodeList[1]).nodeList.Add(node1);

((Node)myGraph.nodeList[1]).nodeList.Add(node3);

((Node)myGraph.nodeList[1]).nodeList.Add(node4);



// 3 - ( 2, 4 )

((Node)myGraph.nodeList[2]).nodeList.Add(node2);

((Node)myGraph.nodeList[2]).nodeList.Add(node4);



// 4 - ( 2, 3 )

((Node)myGraph.nodeList[3]).nodeList.Add(node2);

((Node)myGraph.nodeList[3]).nodeList.Add(node3);

myGraph.Color();

}


}

}





Thanks for any help

PK
 
D

djp

Sorry but i forgot to mention that 'sorting criteria' here is number of
nodes in the ArrayList ( nodeList ) in 'a' and 'b' Nodes.
It returns 2,5,1,3,4.
However:
Node 2 has three elements in nodeList
Node 5 has one element in nodeList
Node 1,2,3 have two elements in nodeList

So they should be sorted as follows:
2,x,y,z,5


This isn't going to work. You are only comparing the count of the
items in the list that the nodes belong to, and returning that. You need
to look at the fields on a and b that you pass in, and then return a value
based on the fields on a and b.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

djp said:
Hi
I have to sort arraylist. I tried to do this using this page as a
reference:
http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm

I did it exactly the same way but unfortunately in opposite to the
example from this link here it does not sort.
I Create Nodes ( color, name ) and add them to nodeList in Graph class.
Then i try to sort..
The code is relatively simple:

namespace NaszaPierwszaListaSasiedztwa

{

class Program

{

public class Graph

{

class CompInv : IComparer

{

// Implement the IComparable interface.

public int Compare(object obj1, object obj2)

{


Node a, b;

a = (Node)obj1;

b = (Node)obj2;

int result = a.nodeList.Count.CompareTo(b.nodeList.Count);

Console.WriteLine(a.nodeList.Count.ToString() + " " +
b.nodeList.Count.ToString() + " " + result.ToString());

return result;

}

}


public ArrayList nodeList = new ArrayList();

public void Color()

{

CompInv comp = new CompInv();

nodeList.Sort(comp);

foreach (Node var in nodeList)

{

Console.WriteLine(var.Name.ToString());

}

}

private ArrayList segregator = new ArrayList();


}

public class Node

{

public ArrayList nodeList = new ArrayList();

public Node(int Color, int Name)

{

this.Color = Color;

this.Name = Name;

}

int Color = -1;

public int Name = -1;

}

/// <summary>

/// Main

/// </summary>

/// <param name="args"></param>

static void Main(string[] args)

{

Graph myGraph = new Graph();

Node node1 = new Node(-1, 1);

Node node2 = new Node(-1, 2);

Node node3 = new Node(-1, 3);

Node node4 = new Node(-1, 4);

Node node5 = new Node(-1, 5);



myGraph.nodeList.Add(node1);

myGraph.nodeList.Add(node4);

myGraph.nodeList.Add(node3);

myGraph.nodeList.Add(node5);

myGraph.nodeList.Add(node2);


// 1 - ( 5, 2)

((Node)myGraph.nodeList[0]).nodeList.Add(node5);

((Node)myGraph.nodeList[0]).nodeList.Add(node2);

// 5 - ( 1 )

((Node)myGraph.nodeList[4]).nodeList.Add(node1);

// 2 - ( 1, 3, 4 )

((Node)myGraph.nodeList[1]).nodeList.Add(node1);

((Node)myGraph.nodeList[1]).nodeList.Add(node3);

((Node)myGraph.nodeList[1]).nodeList.Add(node4);



// 3 - ( 2, 4 )

((Node)myGraph.nodeList[2]).nodeList.Add(node2);

((Node)myGraph.nodeList[2]).nodeList.Add(node4);



// 4 - ( 2, 3 )

((Node)myGraph.nodeList[3]).nodeList.Add(node2);

((Node)myGraph.nodeList[3]).nodeList.Add(node3);

myGraph.Color();

}


}

}





Thanks for any help

PK
 
D

djp

I Solved the problem - Thanks!!
This isn't going to work. You are only comparing the count of the
items in the list that the nodes belong to, and returning that. You need
to look at the fields on a and b that you pass in, and then return a value
based on the fields on a and b.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

djp said:
Hi
I have to sort arraylist. I tried to do this using this page as a
reference:
http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm

I did it exactly the same way but unfortunately in opposite to the
example from this link here it does not sort.
I Create Nodes ( color, name ) and add them to nodeList in Graph class.
Then i try to sort..
The code is relatively simple:

namespace NaszaPierwszaListaSasiedztwa

{

class Program

{

public class Graph

{

class CompInv : IComparer

{

// Implement the IComparable interface.

public int Compare(object obj1, object obj2)

{


Node a, b;

a = (Node)obj1;

b = (Node)obj2;

int result = a.nodeList.Count.CompareTo(b.nodeList.Count);

Console.WriteLine(a.nodeList.Count.ToString() + " " +
b.nodeList.Count.ToString() + " " + result.ToString());

return result;

}

}


public ArrayList nodeList = new ArrayList();

public void Color()

{

CompInv comp = new CompInv();

nodeList.Sort(comp);

foreach (Node var in nodeList)

{

Console.WriteLine(var.Name.ToString());

}

}

private ArrayList segregator = new ArrayList();


}

public class Node

{

public ArrayList nodeList = new ArrayList();

public Node(int Color, int Name)

{

this.Color = Color;

this.Name = Name;

}

int Color = -1;

public int Name = -1;

}

/// <summary>

/// Main

/// </summary>

/// <param name="args"></param>

static void Main(string[] args)

{

Graph myGraph = new Graph();

Node node1 = new Node(-1, 1);

Node node2 = new Node(-1, 2);

Node node3 = new Node(-1, 3);

Node node4 = new Node(-1, 4);

Node node5 = new Node(-1, 5);



myGraph.nodeList.Add(node1);

myGraph.nodeList.Add(node4);

myGraph.nodeList.Add(node3);

myGraph.nodeList.Add(node5);

myGraph.nodeList.Add(node2);


// 1 - ( 5, 2)

((Node)myGraph.nodeList[0]).nodeList.Add(node5);

((Node)myGraph.nodeList[0]).nodeList.Add(node2);

// 5 - ( 1 )

((Node)myGraph.nodeList[4]).nodeList.Add(node1);

// 2 - ( 1, 3, 4 )

((Node)myGraph.nodeList[1]).nodeList.Add(node1);

((Node)myGraph.nodeList[1]).nodeList.Add(node3);

((Node)myGraph.nodeList[1]).nodeList.Add(node4);



// 3 - ( 2, 4 )

((Node)myGraph.nodeList[2]).nodeList.Add(node2);

((Node)myGraph.nodeList[2]).nodeList.Add(node4);



// 4 - ( 2, 3 )

((Node)myGraph.nodeList[3]).nodeList.Add(node2);

((Node)myGraph.nodeList[3]).nodeList.Add(node3);

myGraph.Color();

}


}

}





Thanks for any help

PK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top