Hi.

I'm developing a software for my degree project.

I have some problems in sorting the probability item from linkedList object. Here are the snippets of my code (I have almost 400 lines, so if the below is not enough, please let me know)

``````class LinkedList implements Comparable
{
private char symbol;
private float probability;
private String codeword;
private int flag;

public int compareTo(Object other)
{
return 0;
else if ((this.probability) > ((LinkedList) other).probability)
return 1;
else
return -1;
}

public LinkedList(char s, float p, String c, int f)
{
symbol = s;
probability = p;
codeword = c;
flag = f;
}

public void setProbability(float p)
{
probability = p;
}

public void setCodeword(String c)
{
codeword = c;
}

public void setFlag(int f)
{
flag = f;
}

public float getProbability()
{
return probability;
}
public int getFlag()
{
return flag;
}

public char getSymbol()
{
return symbol;
}
}``````

SOMEWHERE INSIDE CLASS TEST

``````//sort window
{
Test sortWindow = new Test();

sortWindow.setVisible(true);
sortWindow.setSize(500,500);

//JPanel newSortPanel = new JPanel(new GridLayout(symbolNum,2));

for(l = 0; l < symbolNum; l++)
{
}

//after sorting
for(l = 0; l < symbolNum; l++)
{
}

return newSortPanel;
}``````

it says "Cannot find symbol"

Easier to write

``````class LinkedList implements Comparable<LinkedList> {

public int compareTo(LinkedList other) {     // no cast required
return probability - other.probability;
}
}``````

but as probability is a float better to

``````class LinkedList implements Comparable<LinkedList> {

return Float.compare(probability, other.probability);
}
}``````

And I will used Arrays.sort() rather than Collections.sort()
It is an array that you sort

However, I get this error instead:

at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.sort(Arrays.java:1079)
at Test.getSortWindow(Test.java:460)
at Test\$8.actionPerformed(Test.java:376)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton\$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

I guess it contains symbolNum elements but its length is > then that so it has null elements at the end of it.

You will have to make a copy of it into an array of LinkedList[symbolNum] before sending it to sort()

using the constructor

``````//constructor 1
public LinkedList(char s, float p, String c, int f)
{
symbol = s;
probability = p;
codeword = c;
flag = f;
}

//constructor for copyList
{
}``````

and this

``````LinkedList copyList[] = new LinkedList[symbolNum];

for(l = 0; l < symbolNum; l++)
{
System.out.println("Symbol: " + copyList[l].getSymbol() + " Prob: " + copyList[l].getProbability());
}``````

but i still get the same error

Symbol: A Prob: 0.3 <-- the first item is printed
at Test.getSortWindow(Test.java:452)
at Test\$8.actionPerformed(Test.java:388)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton\$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6041)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5806)
at java.awt.Container.processEvent(Container.java:2058)
at java.awt.Component.dispatchEventImpl(Component.java:4413)
at java.awt.Container.dispatchEventImpl(Container.java:2116)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
at java.awt.Container.dispatchEventImpl(Container.java:2102)
at java.awt.Window.dispatchEventImpl(Window.java:2440)
at java.awt.Component.dispatchEvent(Component.java:4243)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)

can anyone help me with this?

i think the problem has been solved and i finally managed to sort the object. thank you very much!

here's the working code

``````LinkedList copyList[] = new LinkedList[symbolNum];

//copy object
for(int j = 0; j < symbolNum; j++)
{