need help in sorting some objects inside objects

i have objects of type Reservation inside objects of type Apartment.
What i ned is jsut to insert object Reservation in the order of their names.
i used this code but it is jsut working for inserting in the begining and the end. It is not working for inserting in the middle
*************************************************************
public boolean makeReservation( String appType, String name, int weekBeg, int weekEnd)
{
Reservation newRes=new Reservation (weekBeg, weekEnd, name);

Appartment cur=head;
Reservation previous=null;

while ((cur.code!=appType)&&(cur!=null))
{
cur=cur.next;
}
if(cur.Res==null)
{
cur.Res=newRes;
newRes.link=null;
}
else
{
while((cur.Res!=null) &&(cur.Res.nameRenter.compareTo(name)<0))
{

previous=cur.Res;
cur.Res=cur.Res.link;
}
if(previous==null)
{
newRes.link=cur.Res;
cur.Res=newRes;
}
else
{
previous.link=newRes;
newRes.link=cur.Res;

}
retrun true;
}

Comments

  • I believe you're looking for something like this:
    [code]
    public void addElement (Apartment b)
    {
    Apartment a = head;
    if (a > b)
    {
    b.tail = head;
    head = b;
    }
    else
    {
    for (a = head; a.tail != null && a.tail < b; a = a.tail);
    b.tail = a.tail;
    a.tail = b;
    }
    }
    [/code]

    It's not the fastest code but i tried to form it so it can be easily implemented into your method.


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    [email protected]
    http://konrads.webbsida.com

  • [b][red]This message was edited by chamster at 2002-10-20 0:34:49[/red][/b][hr]
    I forgot to mention that [red]a < b[/red] will not work. I used it to shorten my code. What i really mean is [blue]a.name.compareTo (b.name)[/blue].

    Also, i think i used Apartment as the little object inside the big one (Reservation). That shouldn't be any problem for you to correct, as the point remains the same. Should you get stuck, just let me know...


    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    [email protected]
    http://konrads.webbsida.com



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion