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