Select Sorting in prompt level

I have below requirement from user.
Prompts:     Loan Id
          Loan Type
          Sort by Account Number/Sale Type
Basically when user run the report, it should prompt below three fields.
Loan Id
Loan Type
Sort order with a drop down list which has values Account Number/ Sale Type.
i.e., user should be able to select sort order he wants for both Account Number/ Sale Type before running the report at
prompt level.
Does anyone know the best way to accomplish this?.
Thanks,
ven
Edited by: Ven Men on Jan 21, 2009 8:32 PM

Hi Deb,
Thanks for your reply. I need little more clarification from you regarding Ascending/Descending and group by.
Please see my comments below.
1) Created a prompt at the universe level where users can choose their sorting option, ie. Ascending or Descending.
    How did you create this. What I did is I created a derived table in Universe with below code.
   "Select distinct table.Account_Number from table order by 1". It just does only Ascending.
   Then I created a prompt, called Sort_Order, in designer as 
   '1'!=@Prompt('Sort_Order','A',{'Account_Number'},mono,Not_Persistent))
  It is not parsed with a Parsing Result as Parse failed: Exception: DBD, ORA-00903: invalid table     name   State: N/A
Since I am not taking this value from database table, I went ahead and created this prompt and exported universe.
  What I understood from your point is I need to create two different prompts, once for Ascending and one for Descending. Please
  let me know if I am going wrong.
2) In the WebI report, create two variables, one for Ascending and Descending. The formulas would be something like:
   cv_Ascending: if(userresponse("Sort order prompt text here")="Ascending";MyField)
   cv_Descending: if(userresponse("Sort order prompt text here")="Descending";MyField)
   I created variable as Sort_Act_Num =if(UserResponse("Sort_Order") = "Account_Number"; [Account Number]; 0).  This just sorts in Ascending order only and when I want to change it to descending, it is not working.
3) Put the cv_Ascending variable in column one set to sort ascending and put cv_Descending variable in column two set to sort descending. (The column with the value that isn't selected is always null so it's sorted on nothing)
4) Since you can't really "hide" columns in WebI, you have to fake it. Set the font color to white (or whatever color your background is) and the width to zero (0) - unfortunately, it won't let you set it to 0, it will default to .02. Turn off the borders. The columns will in effect be "hidden" and disappear.
I've done the same thing with grouping, by creating a variable and using it as a section break. Works like a charm.
For grouping, do I need to create any prompts or variables in designer level? Also in report level, is the code same as above to create variable for grouping.
Please let me know..
Thanks,
Ven Men

Similar Messages

  • Select Sorting at prompt level

    Hi,
       How to select sorting parameter from prompt level?  I have followed the thread [Select Sorting in prompt level  |Re: Select Sorting in prompt level;. I got struck while creating " Prompt - Sort Order" object. How to do this?
    Regards,
    Malini.V

    Hi folks,
    I am a report designer and I use BOXI r2 sp4. After researching the boards, I asked my design team to create the Sort-Order prompt object at the universe level.
    From what I understood, they created it in an independent dervived table. I need to have it joined to another table that contains the "measures" but I don't know what kind of joins should be constructed.
    In the report, I'd like to use it for something like the following:
    >if(Prompt - Sort Order="Descending";cost of service)...
    I would greatly appreciate the advice of the SMEs on how to complete this requirement.
    Wannetta
    Edited by: Wannetta Thomas on May 18, 2009 7:49 PM

  • Displying Current year and previoues year whlie selecting prompt level

    Hi All,
    we have year prompt and report level year column,fact1,Fact2(year Ago).
    now when we select year value in prompt level we need to display that value and privoues year values.
    Ex: if select 2011 in report level need to diplay the 2011,2010.
    report factcolumn is working fine i need to disply the year values(like 2013,2012)
    Find the image
    http://s10.postimg.org/7jvc9svd5/image.png
    Thanks,

    Hi,
    What is your question? what is not working fine? You mean report or Prompt?
    Thanks,

  • LOV custom sorting in prompts for calender month

    Hello Everyone,
    I want to do sorting in prompts in webi for Calender Month in the form (Jan,Feb,Mar....)
    Whereas, it comes alphabetically As (Apr,Aug and so on....) i want to know that can we find the solution for the same.
    I also want to know if yes can we do it in webi itself or we will have to do it on Information Design Tool?

    Hi Foram. I, too, am using HANA as my backend. Here's one way you can sort your data-driven prompts that appear in Web Intelligence:
    Open your business layer within the Information Design Tool.
    Verify that the column to be displayed in the Webi prompt (e.g., "Month") has been defined in your business layer.
    Select the "Parameters and Lists of Values" button.
    In the "List of Values" frame, press the down arrow and then select "List of values based on business layer objects".
    Give the the list an intuitive name; e.g., "Month LOV".
    On the Definition tab, press the "Edit Query..." button.
    Drag the column to be displayed in the Webi prompt (e.g, "Month") to the Result Objects frame.
    Press the "Sort dialog..." button.
    In the Sort dialog box, press the "Insert Sort Object" button.
    Select the column to sort on; e.g., "Month" and then press the OK button.
    Verify the sort type; e.g., "Ascending".
    Press the OK button.
    Press the Preview button to confirm the results.
    Save and publish the universe to your repository.
    Open your Webi report.
    Enter into Design mode.
    Open the Query Panel.
    Select the appropriate query.
    In the Query Filters frame, select the prompt (e.g., "Month") and press the "Show prompt properties" button.
    In the Prompt Properties dialog box, in the Prompt Properties frame, make sure "Prompt with List of Values" is selected.
    Press the OK button.
    Press the Close button.
    Press the Refresh button.
    Confirm that the prompt (e.g., "Month") is sorted properly.
    Hope this helps!
    Chris

  • Is there a way to hide some reports based on the selected values in prompt.

    Hi Experts,
    Is there a way to hide some reports based on the selected values in prompt.
    For ex. if a year is selected in the prompt then the report should display year wise report.
    If a year and half year both are selected in the drop down from prompt section then 2 reports should come.. One for year wise and another for half year wise.Kindly look into this.
    Regards
    Ashish

    Hi,
    Use presentation values in prompts for year,half,qtr and month.Example- For year-y is presentation variable in the same way for halfyear-h,qtr-q and month-m.
    create four intermediate reports.Example-Report r1 with only year column,r2 with only halfyear column,r3 with qtr column and r4 with month column.
    Make column in each report(r1,r2,r3,r4) is equal to their presentation variables(y,h,q,m).
    Use four sections.
    Section1-Place report that should come when only year.
    section2-Place report that should come for year and halfyear.
    Section3-Place report that should come for year,halfyear and qtr.
    Section4-Place report that should come for year,halfyear,qtr and month.
    Apply guided navigation for each section selecting guided navigation-
    For section1-
    properties->Guided navigation->check this Reference Source Request(Yes)->select report r1(year)->check this Show Section(if request returns row)
    In the same way do for remaining section2(select r2),section3(select r3) and section4(select r4)
    Thanks,
    Srikanth
    http://bintelligencegroup.wordpress.com/

  • Regarding Can we store User Selected values in prompts

    Please help us in achieving the below requirement.
    Requirement is to store User Selected values in prompts when the report is opened by a User for the first time and make use of those values to refresh the report next time onwards.
    1.Can this be achieved in WebI ?
    2.If it is thru SDK(we use Java), any experience/help in doing this ?
    Environment : BO XI 3.1 with SAP BW Integration

    Hi ,
    If you dont want data to be displayed in this report for this two values , then exclude this two values when you restrcit your characteristic to the variable.
    If you want not to display this values when user opens help for entering the values where he can see all the possible values for selection for the variable, then it wont be possible.
    Hope this will help.
    - Jaimin

  • Sorting a vector using the selection sort method

    I have to write a program that sorts a Vector using the selection sort algorithm. Unfortunately the textbook only has about 2 pages on vectors so needless to say I'm pretty clueless on how to manipulate vectors. However I think I'm on the right path, however I'm stuck on one part as shown in the code below.     
    private static void  selectionSort(Vector parts)
          int index;
            int smallestIndex;
            int minIndex;
            int temp = 0;
            for (index = 0; index < parts.size() - 1; index++)
              smallestIndex = index;
              for (minIndex = index + 1; minIndex < parts.size(); minIndex++)
               if (parts.elementAt(minIndex) < parts.elementAt(smallestIndex))  // this is where I'm having trouble
                  smallestIndex = minIndex;
                parts.setElementAt(temp, smallestIndex);
                parts.setElementAt(smallestIndex, index);
                parts.setElementAt(index, temp); if (parts.elementAt(minIndex) < parts.elementAt(smallestIndex))
    is returning "ProcessParts3.java:51: operator < cannot be applied to java.lang.Object,java.lang.Object"
    Here is the full program:
    import java.util.*;
    import java.io.*;
    public class ProcessParts3
         static Vector parts;
         public static void main(String[] args)
              loadVector();
         private static void loadVector()
         try
              Scanner fileIn = new Scanner(new File("productionParts.txt"));
              parts = new Vector();
              String partIn;
              while (fileIn.hasNext())
                   partIn = fileIn.nextLine();
                        parts.addElement(partIn.trim());
              selectionSort(parts);
                   for (int i = 0; i < parts.size(); i ++)
                   System.out.println(parts.elementAt(i));
         catch(Exception e)
              e.printStackTrace();
         private static void  selectionSort(Vector parts) //from this part down I'm responsible for the coding, everything
                                                               // everything above this was written by the teacher
                 int index;
            int smallestIndex;
            int minIndex;
            int temp = 0;
            for (index = 0; index < parts.size() - 1; index++)
                smallestIndex = index;
                for (minIndex = index + 1; minIndex < parts.size(); minIndex++)
                    if (parts.elementAt(minIndex) < parts.elementAt(smallestIndex))
                        smallestIndex = minIndex;
                parts.setElementAt(temp, smallestIndex);
                parts.setElementAt(smallestIndex, index);
                parts.setElementAt(index, temp);
    }Edited by: SammyP on Nov 27, 2009 11:43 AM

    SammyP wrote:
    I have to write a program that sorts a Vector using the selection sort algorithm...Hmmm.... Your teacher is, in my humble opinion, a bit of a tard.
    1. Vector is basically deprecated in favor of newer implementations of the List interface which where introduced in [the collections framework|http://java.sun.com/docs/books/tutorial/collections/index.html] with Java 1.5 (which became generally available back in May 2004, and went end-of-support Oct 2009). ArrayList is very nearly a "drop in" replacement for Vector, and it is much better designed, and is marginally more efficient, mainly because it is not syncronised, which imposes a small but fundamentally pointless overhead when the collection is not being accessed across multiple threads (as is the case in your program).
    2. Use generics. That "raw" Vector (a list of Objects) should be a genericised List<String> (a list of Strings)... because it's compile-time-type-safe... mind you that definately complicates the definition of your static sort method, but there's an example in the link... Tip: temp should be of type T (not int).
    Note that String implements [Comparable<String>|http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html], so two String's can safely be compared using the compareTo method... In Java the mathematical operators (<, >, &#43;, -, /, &#42;, etc) are only applicable to the primitive types (byte char, int, float, etc)... The Java Gods just chose to muddy the waters (especially for noobs) by "overloading" the &#43; operator for String (and String only) to enable succinct, convenient string-concatenation... which I personally now see as "a little bit of a mistake" on there part.
         private static void  selectionSort(Vector parts)  {
    int index, smallestIndex, minIndex, temp = 0;
    for (index = 0; index < parts.size() - 1; index++) {
    smallestIndex = index;
    for (minIndex = index + 1; minIndex < parts.size(); minIndex++) {
    if (parts.elementAt(minIndex) < parts.elementAt(smallestIndex)) {
    smallestIndex = minIndex;
    parts.setElementAt(temp, smallestIndex);
    parts.setElementAt(smallestIndex, index);
    parts.setElementAt(index, temp);
    }3. ALLWAYS use {curly braces}, even when not strictly necessary for correctness, because (a) they help make your code more readable to humans, and also (b) if you leave them out, you will eventually stuff it up when you insert a line in the expectation that it will be part of the if statement (for example) but you forgot to also add the now mandatory curly-braces... This is far-too-common source of bugs in noob-code. Almost all professionals, nearly allways allways use curly braces, most of the time ;-)
    4. Variable names should be meaningful, except (IMHO) for loop counters... Ergo: I'd rename index plain old i, and minIndex to plain old j
        for ( int i=0; i<list.size()-1; ++i) {
          int wee = i; // wee is the index of the smallest-known-item in list.
          for ( int j=i+1; j<list.size(); ++j ) {Cheers. Keith.
    Edited by: corlettk on 28/11/2009 09:49 ~~ This here fraggin forum markup friggin sucks!

  • Selection Sorting of Data type (Char)

    public class CharSortTest {
        public static void main(String[] args) {
            char[] letters = {'j', 'a', 'z', 'y', 'x'};
            char min = 0;
            char swap = 0;
            for (int x = 0; x <= letters.length - 1; x++) {
                min = letters[x];
                for (int i = x + 1; i <= letters.length - 1; i++) {
                    if (letters[i] < min) {
                        min = letters;
    if (letters[x] != min) {
    swap = letters[x];
    letters[x] = min;
    min = swap;
    for (int p = 0; p <= letters.length - 1; p++) {
    System.out.println(letters[p]);
    }output:a
    a
    x
    x
    xi followed the logic of selection sorting.. i even checked the integer values of each character in my array
    but why is my program not sorting                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    vajj wrote:
    public class CharSortTest {
    i followed the logic of selection sorting..Obviously you didn't.
    Put in print statements at each step so you can see exactly what's happening--varaibles' values, code paths being followed, etc.

  • Selection Sort troubles

    I am doing a practice exercise involving a Selection Sort. I keep getting 2 errors when I compile and not sure what to do to fix them. Thanks in advance for your help! Here are the two errors:
    SortArray.java:79: illegal start of expression
         private void swapEm ( int indexCheck, int min ){
            ^
    SortArray.java:67: cannot resolve symbol
    symbol  : method swapEm  (int,int)
    location: class SortArray
               swapEm( indexCheck, min );
                     ^Here is my code:
    import java.lang.Math.*;
    public class SortArray {
         * Main method for program.
         * @param  args
        public static void main ( String args[] ){
         int array[] = new int [25]; //variable for array
          * For loop to populate the array with numbers ranging
          * from 0-100 inclusively and then
          * printing them out on the screen.
         for ( int index = 0; index < array.length; index ++ ){
             array[index] =( ( int ) ( Math.random() * 101 )  );
             System.out.println( array[index] );
          * For loop that iterates through array and sorts the array
          * in ascending order.
         int min;
         for ( int indexCheck = 0; indexCheck <  array.length -1;
               indexCheck ++ ){
             min = indexCheck;
             for ( int index = indexCheck + 1; index < array.length;
               index ++ ){
              if ( array[index] < min ){
                   min = array[index];
               swapEm( indexCheck, min );
          * Swaps the values of the array to put them in order.
          * @param one a value of type 'int'
          * @param two a value of type 'int'
         private void swapEm ( int indexCheck, int min ){
             int temp = array[indexCheck];
             array[indexCheck] = array[min];
             array[min] = temp;
         System.out.println();
         for ( int intCounter = 0; intCounter < array.length;
               intCounter ++ ){
             System.out.println( array[intCounter] );
    }//SortArray.java

    This is how i would do it:
    for ( int index = 0; index < array.length; index ++ ){
                   array[index] =( ( int ) ( Math.random() * 101 )  );
                   System.out.println( array[index] );
              System.out.println("-----------");
              /* To sort the array in ascending order
              for (int i=0; i<25; i++) {
                   for (int j=i+1; j<25; j++) {
                        if (array[i] > array [j]) {
                             int temp = array;
                             array[i] = array [j];
                             array[j] = temp;
              for (int i=0; i<25; i++)
                   System.out.println ("" + array[i]);
    All inside your main() method, without even using other methods.
    Calin

  • How to apply sorting for each level of a hierarchy?

    I'm using SSAS OLAP and I want to apply sorting of the levels of a hierarchy.
    I know that I can sort the whole hierarchy via ORDER function (I have some issues when I'm trying to apply DESC sorting
    on the whole hierarchy), but what I really want to achieve is sorting of a specific level. For example in the [Date].[Calendar] hierarchy (Adventure Works Cube), I want to have ASC sorting
    of years, DESC sorting of Quarter, ASC sorting
    of Months, etc. I do not want to break the hierarchy (using BASC or BDESC), I just need them sorted on the same level. Do you have an idea if this is possible at all?
    My idea is to sort the levels based on their key, not by the measure. For example if we have the following structure:
    -2009
     -Q1
       -Jan
       -Feb
     -Q2
       -May
       -June
    -2010
     -Q1
       -Jan
       -Feb
     -Q2
       -May
       -June
    I would like to be able to sort the data in the following order (DESC for years, ASC for Quarters, DESC for months):
    -2010
     -Q1
       -Feb
       -Jan
     -Q2
       -June
       -May
    -2009
     -Q1
       -Feb
       -Jan
     -Q2
       -June
       -May 
    Thanks in advance for your help!

    Hi Rosen,
    According to your description, you want to sort the different hierarchy members in different order, right? Generally SQL Server Analysis Services doesn't provide a way to sort dimension members in Descending order. SSAS provides only Ascending order. We
    can achieve this sort functionality by using a simple trick. However, the order is used for all the members of different hierarchy under this dimension. So I'm afraid that your requirement cannot be achieved. 
    Reference:Sort Dimension Members in DESCending order
    Thank you for your understanding.
    Regards,
    Charlie Liao
    TechNet Community Support

  • Swap Counter in simple Selection Sort method

        void sortSelection(List object)
             int swapCount=0;
               for(int i = list.length; i >= 2; i--)
                 int maxIndex = 0;
                        for(int j = 1; j < i; j++)
                             if(list[j] > list[maxIndex])
                                  maxIndex = j;
                   int temp = list[maxIndex];        //
                   list[maxIndex] = list[i-1];         //         Is the problem here?        
                   list[i-1] = temp;                     //
                   swapCount++;                     //
              System.out.println("\n" +swapCount+ " swaps were needed. ");
        }This is a pretty simple selection sort method. But I am fairly sure that I am incorrectly counting swaps with the code. I have defined a "swap" as when two array indices have their values switched. I am convinced that there is something wrong because the array of numbers is randomly generated, and yet the swapCount counter is always equal
    to (list.length-1) when it prints out. I would think it should be different every time. I indicated the line where I believe the problem lies, but I am not for sure. Thanks everybody.

    List of Random Integers, then sorted:
    9, 29, 30, 42, 53, 58, 59, 64, 66, 69, 74, 79, 79, 87, 95
    9 swaps were needed.then sort again (descending) and get this:
    95, 87, 79, 79, 74, 69, 66, 64, 59, 58, 53, 42, 30, 29, 9
    1 swaps were needed.I'm pretty sure that isn't correct. But forgive me if I did not specify that it was to work in descending order as well.
    So I tried this:
    void sortSelection(List object)
             int swapCount=0;
               for(int i = list.length; i >= 2; i--)
                 int maxIndex = 0;
                        for(int j = 1; j < i; j++)
                             if(list[j] > list[maxIndex])
                                  maxIndex = j;
                  if (maxIndex != (i-1) && ascending==true)
                       int temp = list[maxIndex];
                        list[maxIndex] = list[i-1];                             
                        list[i-1] = temp;   
                        swapCount++;
                  else if (maxIndex != (i+1) && descending==true)
                       int temp = list[maxIndex];
                        list[maxIndex] = list[i-1];                             
                        list[i-1] = temp;   
                        swapCount++;
    } adding the i+1 (because it is going in a different order)...and I think I solved it!

  • Comparable interface selection sort

    Hi,
    I've created a Person Class with a comparable interface. And i've created an ArrayList People with varaibles from the person class in - First_name, Surname, Month(Birthday), Day(Birthday).
    Now i need to use selection sort to sort my arraylist into birthday order. In the Person Class i have a method which gives each person a different number for every possible birthday there is.
    I have created a selction sort but dont know how to call the birthday from my array list and then to sort it.
    This is my code for the selection sort so far:
    import java.util.ArrayList.*;
    public class Main
    public static void selectionSort(Comparable[] people)
    for (int k = people.length-1; k>0; k --)
    Comparable tmp;
    int Large = 0;
    for (int j=1; j<= k; j++)
    if (people[j].compareTo(people[k]) <0)
    Large = j;
    tmp = people[Large];
    people[Large] = people[k];
    people[k] = tmp;
    this method compiles but i need to sort the birthday and dont know how. Also i need to output the whole array in birthday order.
    Any help would be very greatful.
    Thanks
    Dave

    Hi,
    If my understanding is right..
    You are trying to sort with birthday as the primary sort criterria.
    For doing that, you have to write the comparison logic inside the compareTo() method implemented in the Person class (Inherited from Comparable Interface).
    i.e. The compareTo() method should use the the primary_sort_variable(here it is birthday or something derived from it) to return a boolean value for your need.

  • AAAGGGHH!!! Selection sort with external file!

    Hi, i am trying to sort marks from an external file in ascending order, using a selection sort via an existing class StudentMark.
    However, i am receiving 1 error when i compile it!!!!
    code below:
    import java.io.*;
    import java.util.*;
    class sort
         // method to display the contents of an array
         static StudentMark data[];
         static void displayData(int[] data)
              try
                   data = StudentMark.loadMarks();
                   for (int index=0; index != data.length; index++)
                        System.out.println(data[index]+"\t");
              catch(Exception e)
                   System.out.println(e.getMessage());
         static int positionOfLargest(int[] data, int limit)
         // method to return the position of the largest item
         // in the data with bounds 0..limit
              int largest = data[0];
              int indexOfLargest = 0;
              for (int index=1; index <= limit; index++)
                   if (data[index]> largest)
                        largest = data[index];
                        indexOfLargest = index;
              return indexOfLargest;
         public static void selectionSort(int[] data)
         // method to sort the contents of an data into ascending order
              int temporary;
              int position;
              int size=data.length;
              for (int index=size-1; index > 0; index--)
                   position=positionOfLargest(data, index);
                   // swap numbers
                   if (index != position)
                        temporary = data[index];
                        data[index] = data[position];
                        data[position] = temporary;
         public static void main(String[] args)
              int[] data;
              System.out.println("numbers before being sorted\n");
              displayData(data);
              selectionSort(data);
              System.out.println("numbers after being sorted\n");
              displayData(data);
    the error is: sort.java:14: incompatible types -                data = StudentMark.loadMarks();
    ^
    Can anyone see what i am doing wrong??
    Thanks!!

    static StudentMark data[];
    data = StudentMark.loadMarks();the error message "incompatible types" means that the type of data and the return type of StudenMark.loadMarks() are not compatible.
    in case of we don't know the return type of StudentMark.loadMarks() i could only guess:
    class StudentMark {
      public static StudentMark[] loadMarks() {
    }if you used that kind of method declaration you should change your declatation of the variable data:
    public static StudentMark[] data;then your codeline 14 shouldn't be a problem any longer.
    StudentMark[] data and StudentMark data[] ARE different!
    hope it helps.

  • Recursive selection sort stack overflow

    hi, first time posting here.
    i have to write a java method that implements a selection sort. This method must be recursive. ok not a problem. i have come up with this so far
        public int findMin(int index) {
            int min = index - 1;
            if (index < num.length - 1) min = findMin(index + 1);
            if (num[index] < num[min]) min = index;
            return min;
        public void selectionSort(int left) {
            if (left < num.length - 1) {
                swap(left, findMin(left));
                selectionSort(left + 1);
        public void swap(int index1, int index2) {
            int temp = num[index1];
            num[index1] = num[index2];
            num[index2] = temp;
        }which works fine until i toss in a lot of values into the array. this creates a stack overflow, and ive been told that i need to use the divide and conquer method of resolving this issue. I guess this means to split the array up and sort each half seperatly, again and agin, or so im told.
    My question is how do i go about doing this, i am at a loss. Any help at all would be great.
    thank you
    lance

    i get this when i push the array passed about 5500 in sizeI got no problem. Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and http://www.yoda.arachsys.com/java/newsgroups.html
    It is silly to implement selection sort with recursion, double-silly if you use double-recursion as your current code.
    /* silly but no OOME when used against 5500 element array */
    import java.util.*;
    public class RecursiveSelectionSort{
      static int[] num;
      public static int findMin(int index) {
        int min = index; // your code had a bug here
        if (index < num.length - 1){
          min = findMin(index + 1);
        if (num[index] < num[min]){
          min = index;
        return min;
      public static void selectionSort(int left) {
        if (left < num.length - 1) {
          swap(left, findMin(left));
          selectionSort(left + 1);
      public static void swap(int index1, int index2) {
        int temp = num[index1];
        num[index1] = num[index2];
        num[index2] = temp;
      public static void main(String[] args){
        Random rand = new Random();
        int n = 10;
        if (args.length > 0){
          n = Integer.parseInt(args[0]);
        num = new int[n];
        for (int i = 0; i < num.length; ++i){
          num[i] = rand.nextInt(10000);
        selectionSort(0);
        for (int in : num){
          System.out.print(in + " ");
        System.out.println();
    }

  • Selection Sort type iteration with LinkedList

    Hi,
    It seems like Java's ListIterator isn't especially great for doing a "compare every element to every other element" traversion (like the type used in selection sort).
    For example, this code works fine:
    for(ListIterator<T> i = list.listIterator(); i.hasNext();) {
         T a = i.next();
         for(ListIterator<T> j = list.listIterator(j.nextIndex()); j.hasNext();)
              compare(a, j.next());
    }But LinkedList.listIterator(int) is a really poor choice because the iterator has to start at one end and make its way towards the middle before it's returned. So it's not very fast.
    I would do this:
    for(ListIterator<T> i = list.listIterator(); i.nextIndex() < list.size()-1;) {
         T a = i.next();
         ListIterator<T> j = i.clone();
         for(j.next(); j.hasNext();)
              compare(a, j.next());
    }except that ListIterator.clone() isn't defined. There's also no copy constructor like in C++.
    Any ideas?

    Another reason not to use iterators. I don't know
    what those "structural modifications" are but they
    are likely to make the iterators fail via
    ConcurrentModificationException.
    Yeah, I figured that one out pretty quickly.
    And just another comment: you seem to be allowing
    your brain to be fogged by the idea of choosing the
    fastest method. The result of this is that you don't
    get any working methods at all. Don't optimize yet.That's valid. I actually have it working now, but based on the (minimal) experience I've had I always pick the most appropriate collection and then work it out from there.
    Something inside me dies whenever I see remove(0) called on a long ArrayList.

Maybe you are looking for