470,614 Members | 1,569 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,614 developers. It's quick & easy.

ArrayList.Sort not sorting properly

djp
Hi
I have to sort arraylist. I tried to do this using this page as a reference:
http://www.java2s.com/Code/CSharp/Co...eIComparer.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
Mar 21 '07 #1
3 3017
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]
- mv*@spam.guard.caspershouse.com

"djp" <pi*************@gmail.comwrote in message
news:72***************************@news.chello.pl. ..
Hi
I have to sort arraylist. I tried to do this using this page as a
reference:
http://www.java2s.com/Code/CSharp/Co...eIComparer.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


Mar 21 '07 #2
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]
- mv*@spam.guard.caspershouse.com

"djp" <pi*************@gmail.comwrote in message
news:72***************************@news.chello.pl. ..
>Hi
I have to sort arraylist. I tried to do this using this page as a
reference:
http://www.java2s.com/Code/CSharp/Co...eIComparer.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



Mar 21 '07 #3
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]
- mv*@spam.guard.caspershouse.com

"djp" <pi*************@gmail.comwrote in message
news:72***************************@news.chello.pl. ..
>Hi
I have to sort arraylist. I tried to do this using this page as a
reference:
http://www.java2s.com/Code/CSharp/Co...eIComparer.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



Mar 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by jarkkotv | last post: by
6 posts views Thread by Bryon | last post: by
16 posts views Thread by stojilcoviz | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.