tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sérgio Costa" <oscil...@gmail.com>
Subject Re: sort an array
Date Tue, 27 Jun 2006 13:44:35 GMT
This first class should be you domain object.
You should add all related data here.

public class ExampleClass implements Comparable {
    public String astrName;

    /*
     * All other members you need here
     *
     */

    public ExampleClass(String astrName) {
        this.astrName = astrName;
    }

    public int compareTo(Object arg0) {
        ExampleClass obj=(ExampleClass) arg0;
        return astrName.compareTo(obj.astrName);
    }

}

After you have this done, sorting a list of object is as simple as:
public class ExampleMain {
   public static void main(String[] args) {
        List<ExampleClass> listToSort= new ArrayList<ExampleClass>();
        listToSort.add(new ExampleClass("here's one"));
        listToSort.add(new ExampleClass("here's another"));
        listToSort.add(new ExampleClass("just one more"));

        Collections.sort(listToSort);
        //the list is sorted
    }

}

Notice the type checking to make sure you don't put anything other than your
class on your list.

If you're not using Java 5.0, remove the <ExampleClass> from the list
declaration.


On 6/27/06, password password <bernadelpradoclaves@yahoo.es> wrote:
>
> Please, can you put an example? I don't know how put the compareTo Method
> in my servlet.
>
> Sérgio Costa <oscilate@gmail.com> escribió:  Put them in a list and
> Collections.sort() them.
>
> All you'll need to do is create a Class with all those fields implementing
> Comparable, and implement the compareTo Method.
>
> hope it helps
> SC
>
> On 6/26/06, password password wrote:
> >
> > Hi,
> >
> > I have a servlet and in this servlet I have a problem when sort an
> > array. If in Tomcat 5.5.4 (on windows) the order is very fast (about 2
> > seconds) , but in tomcat 5.5.9 is slower (about 25 seconds). I don't
> Know
> > why the same code has diferent time.
> > The code is the next. The array has 7000 elements.
> >
> >
> //////////////////////////////////////////////////////////////////////////////////////////////////////////
> > java.util.Date datTiempoInicio, datTiempoFin;
> > java.util.Calendar calTiempoInicio = Calendar.getInstance();
> > java.util.Calendar calTiempoFin = Calendar.getInstance();
> > long intTiempoUtilizado;
> > datTiempoInicio = new java.util.Date();
> > calTiempoInicio.setTime(datTiempoInicio );
> >
> > if (astrName.length > 1) {
> > //ordenacion de los arrays: astrName[intFilaArray]
> > int i, pasadas;
> > String strTemp;
> > double dblIntensityTemp;
> > double dblBackgroundTemp;
> > int intComparacion;
> > for (pasadas = 1; pasadas < astrName.length; pasadas++) {
> > for (i = 0; i> intComparacion = astrName[i].compareTo(astrName[i+1]);
> > if (intComparacion > 0) {
> > strTemp = astrName[i];
> > astrName[i] = astrName[i + 1];
> > astrName[i + 1] = strTemp;
> > dblIntensityTemp = adblIntensity1[i];
> > adblIntensity1[i] = adblIntensity1[i + 1];
> > adblIntensity1[i + 1] = dblIntensityTemp;
> > dblIntensityTemp = adblIntensity2[i];
> > adblIntensity2[i] = adblIntensity2[i + 1];
> > adblIntensity2[i + 1] = dblIntensityTemp;
> > dblBackgroundTemp = adblBackground1[i];
> > adblBackground1[i] = adblBackground1[i + 1];
> > adblBackground1[i + 1] = dblBackgroundTemp;
> > dblBackgroundTemp = adblBackground2[i];
> > adblBackground2[i] = adblBackground2[i + 1];
> > adblBackground2[i + 1] = dblBackgroundTemp;
> > } // end if (intComparacion > 0)
> > } //end for i
> > } //end for pasadas
> >
> > } // end if (astrName.length > 1)
> >
> > datTiempoFin = new java.util.Date();
> > calTiempoFin.setTime(datTiempoFin );
> >
> > intTiempoUtilizado = Math.abs( (calTiempoFin.getTimeInMillis() -
> > calTiempoInicio.getTimeInMillis()) / (1000) );
> > System.out.println("(ProcessFile) Time = " + intTiempoUtilizado );
> >
> >
> ////////////////////////////////////////////////////////////////////////////////////////////////
> >
> >
> > Can You help me?
> >
> >
> >
> > ---------------------------------
> >
> > LLama Gratis a cualquier PC del Mundo.
> > Llamadas a fijos y móviles desde 1 céntimo por minuto.
> > http://es.voice.yahoo.com
> >
>
>
>
> ---------------------------------
>
> LLama Gratis a cualquier PC del Mundo.
> Llamadas a fijos y móviles desde 1 céntimo por minuto.
> http://es.voice.yahoo.com
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message