Re: Sorting a vector based on another datas



On Jan 31, 3:13 am, "zelao....@xxxxxxxxx" <zelao....@xxxxxxxxx> wrote:
Hey everybody, I'm having troubles with sorting a vector based in
another vector:
Example:
String [] models = {"Anna","Gisele","Yasmin"}
int [] ranking ={3,1,2}
....After sorting it might be this :
models = {"Gisele","Yasmin","Anna"}
ranking = {1,2,3}

It's because i sorted the models' vector based on ranking vector, and
it could accept duplicate datas;
So how can i do that ??????

I think about do a bubble sort in ranking vector and when the
condition met (like ranking[0] > ranking[1]) I swap both vectors, but
I don't think it's the best way. There is another one ?

The best way would be to avoid paralell arrays.
class Model implements Comparable<Model> {
private final int ranking;
private final String name;
public Model(int ranking, String name) {
this.ranking = ranking;
this.name = name;
}
public int compareTo(Model o) {
return ranking < o.ranking ? -1 : ranking == o.ranking ? 0 : 1;
}

public String toString() {
return "Rank " + ranking + ": '" + name + '\'';
}
}


public class SortModels {
public static void main(String...args) {
Model[] models = new Model[] {
new Model(3, "Anna"),
new Model(1, "Gisele"),
new Model(2, "Yasmin")
};
Arrays.sort(models);
for (Model model: models) {
System.out.println(model);
}
}
}

This program will print out:
Rank 1: 'Gisele'
Rank 2: 'Yasmin'
Rank 3: 'Anna'

Hope this helps,
Daniel.

.



Relevant Pages

  • Re: How to get rank?
    ... If you use front end application, do Ranking there ... observations, and their rank. ... CREATE TABLE #Values(val int) ... I can put them into a temp table with an identity column, ...
    (comp.databases.ms-sqlserver)
  • RE: How EXACTLY does Indexing Service determine rank
    ... the point that they are adjacent becoming a phrase and raising the rank ... The ranking mechanism is weighted so that the more highly inflected ... the linguistic engine and ranking algorithm ... Indexing Service is based on ranking formulas that are used everywhere from ...
    (microsoft.public.inetserver.indexserver)
  • Re: Increasing your PAGE RANK
    ... >> Participating in link schemes is a way to increase page rank, ... so the site 'spam ranking' will ... I have a "useful links" page on one site for people who want ... > to do a link exchange. ...
    (alt.internet.search-engines)
  • Re: Integrating results from different pages? (No luck w/ INDEX &
    ... ranking for every player that has a ranking. ... Most of my source come in the format "Rank. ... Lastname" match a cell that is a formula that gives the same result? ... > Now you have all the names in sheet3 ...
    (microsoft.public.excel.misc)
  • RE: Ranking different groups in one column
    ... Thank you for the ranking different groups within one colmun formula: ... As you see, three regions, a bunch of store names and scores. ... Gary''s Student - gsnu200776 ... a.n.other user from this datasheet and just 'pulling' the rank no into the ...
    (microsoft.public.excel.misc)