Java Forum / General / February 2007
Sorting a vector based on another datas
zelao.itu@gmail.com - 31 Jan 2007 11:13 GMT 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 ?
Lew - 31 Jan 2007 13:00 GMT > Hey everybody, I'm having troubles with sorting a vector based in > another vector: [quoted text clipped - 4 lines] > models = {"Gisele","Yasmin","Anna"} > ranking = {1,2,3} These are vectors, but not Vectors. The conventional Java term for these structures is "array".
> It's because i sorted the models' vector based on ranking vector, and > it could accept duplicate datas; [quoted text clipped - 3 lines] > 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 ? You'd be better off declaring a class type that holds the value and rank, then sort an array or List of that type based on a Comparator that examines the rank.
- Lew
zelao.itu@gmail.com - 31 Jan 2007 17:17 GMT Could I insert these values in a Map Collection and then sort it by any term that I choose
Andreas Leitgeb - 31 Jan 2007 17:41 GMT > Could I insert these values in a Map Collection and then sort it by > any term that I choose Yes, but you can also sort the Vector elements directly (without shuffling around between various collection types) by implementing your specific Comparator derived class, which implements compare(x,y) according to the order you have in mind, and passing an instance of your Comparator to Collections' sort(List<T> list, Comparator<? super T> c) method. (yes, a Vector is also a List)
Kai Schwebke - 31 Jan 2007 17:58 GMT > Hey everybody, I'm having troubles with sorting a vector based in > another vector: [quoted text clipped - 4 lines] > models = {"Gisele","Yasmin","Anna"} > ranking = {1,2,3} Just put the strings as values in a TreeMap using the ranking as key. The TreeMap then may be accessed sorted by the key ordering (e.g. the values() method returns an ordered collection).
If you have a large number of elements bubble sort will be very slow -- it is the worst method to sort any kind of data.
Kai
Daniel Pitts - 31 Jan 2007 18:29 GMT > zelao....@gmail.com wrote: > > Hey everybody, I'm having troubles with sorting a vector based in [quoted text clipped - 14 lines] > > Kai Mostly correct. There are worse sorts than bubble sort. Permutation sort, for example. Try every permutation until you find one that is sorted.
Chris Uppal - 31 Jan 2007 19:14 GMT > > If you have a large number of elements bubble sort will be very slow -- > > it is the worst method to sort any kind of data. [quoted text clipped - 4 lines] > Permutation sort, for example. Try every permutation until you find > one that is sorted. Or Random Sort (aka BogoSort)...
My personal favourite, though, is Non-Sort. Here's a Java implementation in case anyone needs it:
/** * Non-sort, the fastest known probabilistic sorting algorithm. * * This is a probabilistic algorithm, and so is <em>not guaranteed</em> * to return the correct answer, but — unlike many sorting * algorithms — it <em>is</em> guaranteed that its runtime will * not be affected by the order of the input elements. Indeed, the runtime * is O(1) regardless of the input. However — and again, unlike * many sorting algorithms — the order of the input <em>does</em> * influence the probability of the algorithm producing the correct answer, * reaching 100% for fully sorted inputs. * * @param list the list to be sorted. * @return the sorted list. Callers should not assume this * will be != to the input list. */ public static <X> java.util.List<X> nonSort(java.util.List<X> list) { return list; }
I also have an in-place version (plug-compatible with java.util.Collections.sort(List)), if required.
-- chris
Ian Wilson - 01 Feb 2007 10:51 GMT >>>If you have a large number of elements bubble sort will be very slow -- >>>it is the worst method to sort any kind of data. [quoted text clipped - 34 lines] > I also have an in-place version (plug-compatible with > java.util.Collections.sort(List)), if required. You forgot to specify the licence. GPL? BSD? Commercial? If someone uses it in a commercial product do you want attribution? "Sort function provided by Chris Uppal" ;-)
zelao.itu@gmail.com - 01 Feb 2007 15:44 GMT Thanks a lot for everybody!!!! it's really solved my problem!!!
thanks!!
**Non-Sort!!! LOL!!! hehe =D
Chris Uppal - 02 Feb 2007 13:03 GMT [me:]
> > public static <X> java.util.List<X> > > nonSort(java.util.List<X> list) > > { > > return list; > > } [...]
> You forgot to specify the licence. GPL? BSD? Commercial? > If someone uses it in a commercial product do you want attribution? > "Sort function provided by Chris Uppal" > ;-) I did consider that, but it's difficult to know how to phrase the licence...
================ Copyright (c) Chris Uppal 2007. Permission is hereby granted to use and distrubute the software for all purposes subject to the following conditions:
NO WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO (etc, etc, etc -- you know the rest).
The above copyright notive, this license text, and the name "Chris Uppal", must NOT appear in any copies of this software. If the program in which it is embedded is such that a user may review licensing and copyright attribution; then credit must NOT be given to "Chris Uppal" and that name must NOT appear in any form or as any part of that information. ================
But it seems a little clumsy -- and in any case would be incompatible with the licensing terms of other software I've made available. (Would also be incompatible with the GPL, but who cares...)
-- chris
Randolf Richardson - 03 Feb 2007 08:33 GMT [sNip]
>> You forgot to specify the licence. GPL? BSD? Commercial? >> If someone uses it in a commercial product do you want attribution? [quoted text clipped - 6 lines] > ================ > Copyright (c) Chris Uppal 2007. [sNip]
> NO WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED > TO (etc, etc, etc -- you know the rest). > > The above copyright notive, this license text, and the name "Chris [sNip]
Whoops: notive
> But it seems a little clumsy -- and in any case would be incompatible > with the licensing terms of other software I've made available. You need get in touch with a lawyer about the wording. If you don't do this, then you might not have things covered properly in the spirit you intended.
> (Would also be incompatible with the GPL, but who cares...) How so? Because you don't want credit for it? By the way, I don't care either. =D
 Signature Randolf Richardson - kingpin+nntp@lumbercartel.ca The Lumber Cartel, local 42 (Canadian branch) http://www.lumbercartel.ca/
Daniel Pitts - 31 Jan 2007 18:37 GMT On Jan 31, 3:13 am, "zelao....@gmail.com" <zelao....@gmail.com> wrote:
> Hey everybody, I'm having troubles with sorting a vector based in > another vector: [quoted text clipped - 12 lines] > 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.
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|