[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How do I sort a String Array based on a key of substrings?

Posted on 2004-10-11
8
Medium Priority
?
335 Views
Last Modified: 2010-03-31
I create a key of substrings from a String Array.  I want to sort my String Array based on the key I created.


String Array example:                       key of substrings:
123  xxxxxx  456 xxx 789 xxxxxx        123456789
123  xxxxxx  789 xxx 456 xxxxxx        123789456
123  xxxxxx  456 xxx 789 xxxxxx        123456789
123  xxxxxx  789 xxx 456 xxxxxx        123789456

Desired output:
123  xxxxxx  456 xxx 789 xxxxxx        
123  xxxxxx  456 xxx 789 xxxxxx        
123  xxxxxx  789 xxx 456 xxxxxx        
123  xxxxxx  789 xxx 456 xxxxxx

Any suggestions???      

I'm rather new to Java so please explain and show code...



0
Comment
Question by:seckel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 12

Expert Comment

by:Giant2
ID: 12277467
use the sort method and create your own Comparator.
See here:
http://www.javaalmanac.com/egs/java.lang/Sortable.html
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12277498
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12277517
Hi,
if you put your data into an array list, you can sort the same using the Collections.sort
        ArrayList result= new ArrayList();
result.add("456");
result.add("123");
        Collections.sort(result);


else you can implement the comparable interface for your custom class/value object

class Content implements Comparable {

     * @param other: Any object to be compared with.
     * @return true if the all the attributes of the two answer
     *             objects are the same
     */
    public boolean equals(Object other)
    {
          if (this==other) return true;
          if (other==null) return false;
          if (this.getClass() != other.getClass() ) return false;
          Content sameClass = (Content)other;
              return this.author.equals  (sameClass.author)&&
               this.title.equals(sameClass.title) &&
    }

      public int compareTo(Object o) {
            Content c = (Content)o;
//you can compare the title or all of your variables in ur class
            return this.title.compareTo(c.title);
      }



hope this helps.

Kiran
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Giant2
ID: 12277532
see even here:
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

Hope these link could help you.
Bye, Giant.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12277878
Use this idea
import java.util.*;

public class SortTest {

  public static void main(String s[]) {
    String[] arr1 = {
        "123", "456", "789"};
    String key = "123456789";
    String[] arr2 = {
        "123", "456", "123"};
    String key1 = "123456123";
    SortArray array = new SortArray(arr1, key);
    array = new SortArray(arr2, key1);
    Vector vect = array.getArray();
    System.err.println(vect.size());
    SortArray[] sortArray = (SortArray[]) vect.toArray(new SortArray[0]);
    java.util.ArrayList vect1 = Collections.list(sortArray[0].ht.keys());
    Collections.sort(vect1);
    for(int i=0;i<vect1.size();i+=1)
    {
     
     for(int j=0;j<sortArray.length;j++)
     {
     
       
       if(vect1.get(i).toString().equals(sortArray[j].key))
       {
         System.err.print("Key :" + sortArray[j].key+" ");
         for(int k=0;k<sortArray[j].array.length;k++)
         {
           System.err.print(sortArray[j].array[k]+" ");
         }
         System.err.println();
       }
     }
    }

  }

}

class SortArray {
  static final Vector vect = new Vector();

  SortArray(String[] array, String key) {
    this.array = array;
    this.key = key;
    vect.add(this);
    ht.put(key, key);
  }

  Vector getArray() {
    return vect;
  }

  String[] array = null;
  String key = null;
  static final Hashtable ht = new Hashtable();
}
0
 
LVL 12

Accepted Solution

by:
Giant2 earned 2000 total points
ID: 12284125
The basically idea of Comparator is:
You have a collection (an Array in your case).
Create a class Comparator that say when one object is less, equal, greater than other (see the link I post).
Your data are formed by a key(to sort) and a data. So you can create an object representing this structure. When use Collection.sort(,..yourComparator), your Comparator must decide is one object is less... than other. And the job is done!

Sorry, but we cannot post a full code (it's out of EE mission). We can give you ideas and eventually check why your code not goes.

Hope this could help you.
Bye, Giant.
0
 

Author Comment

by:seckel
ID: 12286204
Gaint2

I'll try your idea today and let you know....
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12295000
Thanks.
:)
Happy to help you.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question