Bob J
Bob J

Reputation: 333

Sort components of a vector numerically which contain letter numbers and letters

Is there any way to sort a vector numerically?(i wanna sort the number before the first ; (semicolon)

I need let's say this (it's a vector with 4 String/components)

[
 7394;dasd;dasda;dasda;5;3  

 2222;dasdasd;das;true;7;4;dsda;60  

 6660;dsada;dasasd;true;6;3 

 2345;dasdsagfd;das;true;7;4;gfgfdgd;60
]

to become this

[
 2222;dasdasd;das;true;7;4;dsda;60

 2345;dasdsagfd;das;true;7;4;gfgfdgd;60  

 6660;dsada;dasasd;true;6;3  

 7394;dasd;dasda;dasda;5;3
]

or this [3123;dasdas;31;31 1115;das;31;312 4412;sdf;31;42]

to [1115;das;31;312 3123;dasdas;31;31 4412;sdf;31;42] (im sorting 3123, 1115, and 4412 numerically but i still keep the things after)

I've thought of converting each components to a string and then doing something like:

int count;
for(int i=0;i<string_component1.length();i++){
    if(Character.isDigit(string_component1.charAt(i)){
    count = i;
    break;
   }
}

and then with substring i would take the part i want, put it on a string, convert it to an int, then i would take the first one(lowest), use vector contains to find in which components its in and take this components to put it on a new vector at the first position. and so on with the others components

but i think its too much code for nothing and it wouldn't work since the vector size can be 3 or 50.

Upvotes: 0

Views: 179

Answers (4)

Bohemian
Bohemian

Reputation: 424993

Here's the elegant way of doing it using Collections.sort() with a Comparator:

Vector<String> vector;

Collections.sort(vector, new Comparator<String>() {
  public int compare(String s1, String s2) {
    return getInt(s1) - getInt(s2);
  }
  int getInt(String s) {
    return Integer.parseInt(s.replaceAll("(\\d+).*", "$1"));
  }
});

Upvotes: 1

Andreas Fester
Andreas Fester

Reputation: 36630

Is there any way to sort a vector numerically?(i want to sort the number before the first ; (semicolon) )

The general pattern to sort a java.util.Vector is to implement a Comparator and pass it to the Collections.sort() method. All the logic for sorting order can then be put into the compare() method of the Comparator. In your case, it could look like this:

public static void main(String[] args) {
  String[] input = {"7394;dasd;dasda;dasda;5;3", "2222;dasdasd;das;true;7;4;dsda;60",
                    "6660;dsada;dasasd;true;6;3", "2345;dasdsagfd;das;true;7;4;gfgfdgd;60"};
  Vector<String> vec = new Vector<>();
  vec.addAll(Arrays.asList(input));
  System.out.println("Input : " + vec);

  Collections.sort(vec, new Comparator<String>() {

     @Override
     public int compare(String o1, String o2) {
        int i1 = Integer.valueOf(o1.split(";")[0]);
        int i2 = Integer.valueOf(o2.split(";")[0]);

        return i1 - i2;
     }
  } );

  System.out.println("Result: " + vec);      
}

Output:

Input : [7394;dasd;dasda;dasda;5;3, 2222;dasdasd;das;true;7;4;dsda;60, 6660;dsada;dasasd;true;6;3, 2345;dasdsagfd;das;true;7;4;gfgfdgd;60]
Result: [2222;dasdasd;das;true;7;4;dsda;60, 2345;dasdsagfd;das;true;7;4;gfgfdgd;60, 6660;dsada;dasasd;true;6;3, 7394;dasd;dasda;dasda;5;3]

Upvotes: 2

Bogdan Emil Mariesan
Bogdan Emil Mariesan

Reputation: 5647

If you strings have a fix pattern you could do something like:

int indexOfSemi = string_component.indexOf(";")

With this you have the first semicolon. And then with:

String myNumber = string_component.substring(0,indexOfSemi-1)

And now you have your number which you must convert to int. Do this in a Comparator class and you have your comparing mechanism.

Upvotes: 0

BobTheBuilder
BobTheBuilder

Reputation: 19284

I couldn't understand your sorting order, but you can sort a collection using a Collection.sort(),

You can use Collection.sort() to sort your list and implement a custom Comparator to order elements the way you want.

Upvotes: 0

Related Questions