Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2003
    Location
    London
    Posts
    341

    Question Unanswered: JAVA - Search Page and Sort

    Hi I have just created a search page which consists of roughly 200 arrays covering URL's, Titles and Keywords. Many of the arrays use the same search keywords and the results turn out into a long drop down list of entries.

    Is there anyway that I can sort the results into alphabetical order by simply adding a new script and does anybody have a script I can use?

    Christy

  2. #2
    Join Date
    Aug 2003
    Posts
    40
    Do you want to sort the array contents in Java and then display the
    results or are you looking at having the results sorted in Javascript if
    more options are added within the page.

    For java, you can use a bubble sort which is easy but inefficient. A
    "selected" sort can also be used but requires more code. Here's an
    example of a simple "bubble" sort that works/returns a simple String[]...

    PHP Code:
    public class myClass {
      public static 
    String[] sortArray(String[] arrayIn) {
        
    String temp "";
        
    boolean unsorted false;

        do {
          
    unsorted false;
          for (
    int i=0i<arrayIn.length-1i++) {
            if (
    arrayIn[i].compareTo(arrayIn[i+1]) > 0) {
              
    temp arrayIn[i];
              
    arrayIn[i] = arrayIn[i+1];
              
    arrayIn[i+1] = temp;
              
    unsorted true;
            }
          }
        }
        while(
    unsorted);
        return(
    arrayIn);
      }

      public static 
    void main(String[] args) {
        
    String[] myString = {"Hello","Goodbye","Text","Animal","Farmer","Mouse"};

        for (
    int i=0i<myString.lengthi++) {
          
    System.out.println(myString[i]);
        }

        
    myString sortArray(myString);

        for (
    int i=0i<myString.lengthi++) {
          
    System.out.println(myString[i]);
        }
      }

    For JS, you can use the sort() function available for array objects.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •