Sorting a vector based on another datas

Discussion in 'Java' started by zelao.itu@gmail.com, Jan 31, 2007.

  1. Guest

    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 ?
     
    , Jan 31, 2007
    #1
    1. Advertising

  2. Lew Guest

    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}


    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;
    > 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 ?


    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
     
    Lew, Jan 31, 2007
    #2
    1. Advertising

  3. Guest

    Could I insert these values in a Map Collection and then sort it by
    any term that I choose
     
    , Jan 31, 2007
    #3
  4. <> wrote:
    > 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)
     
    Andreas Leitgeb, Jan 31, 2007
    #4
  5. Kai Schwebke Guest

    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}


    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
     
    Kai Schwebke, Jan 31, 2007
    #5
  6. Daniel Pitts Guest

    On Jan 31, 9:58 am, Kai Schwebke <> wrote:
    > 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}

    >
    > 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


    Mostly correct. There are worse sorts than bubble sort.
    Permutation sort, for example. Try every permutation until you find
    one that is sorted.
     
    Daniel Pitts, Jan 31, 2007
    #6
  7. Daniel Pitts Guest

    On Jan 31, 3:13 am, "" <> 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.
     
    Daniel Pitts, Jan 31, 2007
    #7
  8. Chris Uppal Guest

    Daniel Pitts wrote:

    > > 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

    >
    > Mostly correct. There are worse sorts than bubble sort.
    > 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 &mdash; unlike many sorting
    * algorithms &mdash; 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 &mdash; and again, unlike
    * many sorting algorithms &mdash; 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
     
    Chris Uppal, Jan 31, 2007
    #8
  9. Ian Wilson Guest

    Chris Uppal wrote:
    > Daniel Pitts wrote:
    >
    >
    >>>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

    >>
    >>Mostly correct. There are worse sorts than bubble sort.
    >>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 &mdash; unlike many sorting
    > * algorithms &mdash; 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 &mdash; and again, unlike
    > * many sorting algorithms &mdash; 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.
    >


    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"
    ;-)
     
    Ian Wilson, Feb 1, 2007
    #9
  10. Guest

    Thanks a lot for everybody!!!!
    it's really solved my problem!!!

    thanks!!

    **Non-Sort!!! LOL!!! hehe =D
     
    , Feb 1, 2007
    #10
  11. Chris Uppal Guest

    Ian Wilson wrote:

    [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
     
    Chris Uppal, Feb 2, 2007
    #11
  12. On Fri, 02 Feb 2007 05:03:37 -0800, Chris Uppal
    <-THIS.org> wrote:
    > Ian Wilson wrote:

    [sNip]
    >> 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.

    [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

    --
    Randolf Richardson -
    The Lumber Cartel, local 42 (Canadian branch)
    http://www.lumbercartel.ca/
     
    Randolf Richardson, Feb 3, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison
    Replies:
    0
    Views:
    469
    John Harrison
    Aug 29, 2003
  2. Kevin Goodsell
    Replies:
    0
    Views:
    380
    Kevin Goodsell
    Aug 29, 2003
  3. DurumDara
    Replies:
    2
    Views:
    266
    Marc 'BlackJack' Rintsch
    Apr 1, 2006
  4. Replies:
    8
    Views:
    1,939
    Csaba
    Feb 18, 2006
  5. Replies:
    2
    Views:
    1,441
    James Kanze
    Jul 6, 2010
Loading...

Share This Page