#### Archived

This topic is now archived and is closed to further replies.

# [java] Sorting algorithym

This topic is 5945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys I have a quick question about a sorting routine I made. It seems to work fine when the 1st element in the vector list it is sorting is in existence within the string it is sorting( as long as that element comes after all of the other elements in the list) well here I'll post the code and then do some more explaining. code:
  private tag getMostImmediate(String s1){ int LastTagPos = -50; int tagIndex=50; tag tmpT = new tag(); //Loop through all tags finding most immediate for(int j=0; j < tags.size(); j++){ tmpT.copy((tag)tags.elementAt(j)); if(LastTagPos == -50){//Set the first tag position and index LastTagPos = tmpT.tagPos; tagIndex = j; } if(tmpT.tagPos < LastTagPos && tmpT.tagPos > -1){//This tag is more immediate LastTagPos = tmpT.tagPos; tagIndex = j; } } return (tag)tags.elementAt(tagIndex); } 
explanation: tags <- a vector list of tag objects tag.tagPos <- int val representing the most immediate position of that tag within the string.
-1 if non-exisitant, otherwise always a positive int.
what this routine needs to do is find the tag that has the lowest .tagPos value but is not equal to -1. now this list of tag objects does have the correct .tagPos value I've triple checked this, but for some reason when the tags.elementAt(0).tagPos == -1 , and the any of the other elements .tagPos != -1 it seems to return the wrong tag object(it actually returns the object at index zero). Like I said I'm fairly certain I've narrowed down to this. If you would like to see an example of the parser in action you can go to http://216.70.7.1/chatdev/chatapplet/index.html the console displays some relevant debugging info too. although if anyone would like to see any more of the source I would be more that happy to provide it. I think that once this problem is fixed and I have optimized it I will have a very robust parsing tool at my disposal. Any and all comments are welcome, and thanks in advance. War doesn't determine who is right, war determines who is left. Edited by - wrathnut on November 12, 2001 11:24:32 AM

##### Share on other sites
Most likely it has something to do with this line.

tmpT.copy((tag)tags.elementAt(j));

What does the tag.copy(tag t) function look like? Does it return a new copy, or does it alter the ''this'' pointer. My guess is that it returns a new copy of some Object and you are not catching it, therefore, your LastTagPos is being set to tmpT.tagPos (0) the first time, then short circuiting the if statements every other time through the loop.

##### Share on other sites
Here is the copy function and I'll also show the constructor so there is an idea of what the object is.

Copy function:
  public void copy(tag t){ this.tagText = t.tagText; this.tagPos = t.tagPos; this.fntVal = t.fntVal;}

constructor:
  tag(){//Defualt constructor this.tagText = new String(); this.tagPos = -1; this.fntVal = 0;} tag(String s1, int fontVal){//constructor this.tagText = new String(s1); this.tagPos = -1; this.fntVal = fontVal;}

I don't know if the copy function is doing what I think it should but I hope so

War doesn't determine who is right, war determines who is left.

Edited by - wrathnut on November 12, 2001 11:50:52 AM

##### Share on other sites
After doing some more testing on my applet I was able trick the parser into working by adding a zero index tag to the very end of the message but it still bothers the hell out of me because as far as I can see that routine should be working.

If this helps any I am going to post links to all of the class sources here:

ChatApplet.java

connector.java

line.java

lineSegment.java

tag.java

windowC.java

winParser.java

basic breakdown of the logic...

ChatApplet the applet that implements these classes.
windowC is a text window object, it is composed of lines and other window data.
line s are composed of line segments.
lineSegment s are determined by tags and line segment length. They contain font data and text and raw text.
winParser parses a portion of a string into a line segment for a line
connector just retrieves text data from a local url.

The problem is in the winParser object in a method called parseSegment, which is where the getMostImmediate() function is called.

I know this is asking a lot but I am totally stumped, considering I've done stuff that is 100 times harder I can't believe a little thing like this has got me stumped. Hopefully a second pair of eyes will help I don't know.

One thing I noticed if you are trying to get the links inside netscape it tries to download them as a class file, they are actually the .java files.. if you are using netscape just force the download to be a .java file

Edited by - wrathnut on November 13, 2001 11:14:21 AM

##### Share on other sites
Hey Jim,

Thanks for taking a look at this baby. I finally figured out what was wrong. It was short circuiting but not because of the object type being returned. Just when it checked to so if the previous tag was greater. Anyway thanks again for your help!

In the words of Billy Madison, "I am the smartest man alive!" Or the stupidest for not have noticing earlier...