Efficient datastructure to sort on different fields

Hi,
I have to write one program where there are different fields like item number,cost,sales qty,sales price,gross margin etc.
I need to rank the items depending upon its sold quantity ,sold price & gross margin.
Moreover there will be power ranking which depends upon average of other 3 rankings.
Which datasture will be suitable for this ? Should I use one class Item with all these fields & take array of objects of this class & sort the array
1. first depending upon qty & add unit ranking to class
2. sort the array depending upon sold price & add sell ranking
3. sort the array depending upon gross margin & add gross margin ranking .
4. Then take average of this & accordingly add power ranking.
Or will it be efficient to write four queries to get data from database sorted by each condition respectively & add the whole data to hashtable.
Please help me in this. I am bit concerned because all my reports are of this kind. For all reports I am using hashtable with one key & corrosponding vector? Is it efficient or should I use class & keep objects of this class in hashtable?
Regards,
Veena

Hi All,
Thanks a lot for your response. I am writing a class & keeping objects of these in an array, sorting it four times & accordingly adding ranking to the class. Objects can be more than 1000 but still instead of hitting database 4 times to get data ordered by 4 criterias I am finding it efficient to sort the arrays. Do you agree with me?
I can not do it in the database using stored procedure because user can choose items of any vendorsat a time e.g. he can choose 2 vendors' items &nedd to see ranking only among these 2 vendors.
Thanks,
Veena
Thanks a lot.

Similar Messages

  • Sort an object by different fields

    Hello!
    Sorry for the (maybe) bad englisch but I'm a German.
    If I have an object fith different fields for example field1, field2, field3.
    To write a method which sorts an array of this objects by a given field?
    I mean something like this
    sort(String field, object[] o) {
    //sort by field
    instead of this
    sortbyfield1(object o){
    //sort by "field1"
    sortbyfield2(object[] o){
    //sort by "field2"
    hope you understood
    thanks
    Stefan

    When faced with this problem I usually use a Comparator helper class. Depending on whether the Comparator can be re-used I'll create a separate class or if not an inner or anonymous inner class. For example:
    import java.util.Comparator;
    import java.util.Arrays;
    import java.util.List;
    import java.util.Collections;
    public class Main {
      // Compares Fields.f1 ascending
      public Comparator c1 = new Comparator() {
        public int compare(Object o1,
                           Object o2) {
          return ((Main.Fields)o1).f1.compareTo(((Main.Fields)o2).f1);
      public void sortem() {
        // Create our array of objects
        Object[] array = new Object[] {
          new Fields(new Integer(1),3,"3"),
          new Fields(new Integer(2),2,"1"),
          new Fields(new Integer(3),1,"2")
        // Sort on Field.f1 using reusable comparator
        Arrays.sort(array,c1);
        System.out.println(Arrays.asList(array));
        // Sort on Field.f2 using inner class
        Arrays.sort(array,new C2());
        System.out.println(Arrays.asList(array));
        // Sort on f3 using an anonymous inner class
        Arrays.sort(array, new Comparator(){
          public int compare(Object o1,
                           Object o2) {
            return((Main.Fields)o1).f3.compareTo(((Main.Fields)o2).f3);
        System.out.println(Arrays.asList(array));
      public static void main(String[] args) {
        Main m = new Main();
        m.sortem();
      // Class with many fields of different types
      class Fields {
        public Integer f1;
        public int     f2;
        public String  f3;
        public Fields(Integer f1, int f2, String f3) {
          this.f1 = f1;
          this.f2 = f2;
          this.f3 = f3;
        public String toString() {
          return ""+f1+" "+f2+" "+f3;
      // Inner class comparator on Field.f2
      class C2 implements Comparator {
        public int compare(Object o1,
                           Object o2) {
          int result = 0;
          int i1 = ((Main.Fields)o1).f2;
          int i2 = ((Main.Fields)o2).f2;
          if (i1==i1) result = 0;
          if(i1<i2) result = -1;
          else if (i1>i2) result = 1;
          return result;

  • Is it possible to order a group by a different field

    I have a report that groups everything perfectly by Type.  The issue I am having is trying to order the values returned in each grouping by a different field in the returned data, it is a number.
    Is it possible to sort a group based on a field in the returned data, without having to create another group?

    Never mind, just made the the second field a sub group

  • Split data into different fields in TR

    I have a flat file with space (multiple spaces between different fields) as a delimiter. The problem is, file is coming from 3rd party and they don't want to change the separator as comma or tab delimited CSV file. I have to load data in ODS (BW 3x).
    Now I am thinking to load line by line and then split data into different objects in Transfer rules.
    The Records looks like:
    *009785499 ssss BC sssss 2988 ssss 244 sss 772 sss  200
    *000000033 ssss AB ssss        0  ssss   0 ssss 0 ssss 0
    *000004533 ssss EE ssss        8  ssss   3 ssss 2 ssss 4
    s = space
    Now I want data to split like:
    Field1 = 009785499
    Field2 = BC
    Field3 = 2988
    Field4 = 244
    Field5 = 772
    Field6 = 200
    After 1st line load, go to 2nd line and split the data as above and so on. Could you help me with the code pleaseu2026?
    Is it a good design to load data? Any other idea?
    I appreciate your helps..

    Hi,
    Not sure how efficient this is, but you can try an approach on the lines of this link /people/sap.user72/blog/2006/05/27/long-texts-in-sap-bw-modeling
    Make your transfer structure in this format. Say the length of each line is 200 characters. Make the first field of the structure of length 200. That is, the length of Field1 in the Trans Struc will be 200.
    The second field can be the length of Field2 as you need in your ODS, and similarly for Field3 to Field6. Load it as a CSV file. Since there are no commas, the entire line will enter into the first field of the Trans Structure. This can be broken up into individual fields in the Transfer Rules.
    Now, in your Start Routine of transfer rules, write code like this (similar to the ex in the blog):
    Field-symbols <fs> type transfer-structure.
    Loop at Datapak assigning <fs>.
        split <fs>-Field1 at 'ssss' into <fs>-field1 <fs>-field2 <fs>-field3....
        modify datapak from <fs>
    endloop.
    Now you can assign Field1 of Trans Struc to Field1 of Comm Struc, Field2 of Trans Struc to Field2 of Comm Struc and so on.
    Hope it helps!
    Edited by: Suhas Karnik on Jun 17, 2008 10:28 PM

  • Is it possible to do sorting on multiple field inside a GROUP

    Hi
    I am wondering if it is possible to do a multiple sorting on multiple column/fields?
    When I right click on Change Group on the report
    on the "Change Group Options" window, it only has one field which allow you to sort in ascending/ descending order.
    I notice that it also has another checkbox which called Use a formula as Group Sort order, I am not quite sure how to use it to sort multifield/column.
    E.g.
    I have a table which has 5 fields
    I would like to sort it by TotalSpend, TotalEarned, Department, ...etc
    I would be grateful, if you can show me how to do a group sorting for multi column/fields
    Thanks in advance.

    To Sastry,
    Thanks for your information, unfortunately, I am using Crystal report XI.
    To Sandra,
    <<Yes, go into the Group expert and select them in the order in which you want them sorted>>
    Do you mean create different group to do the sorting?
    Could you show me how to create a formula which allow crystal report to do the sorting on multi-field.
    Thanks in advance.

  • How can we sort up currency field in alv grid??

    Hi GUrus,
    Can any one suggest me how to sort the currency field in alv grid...Please help me out of this issue..
    Thanks in advance!!!
    regards,
    Kranthi.

    hii,
    SAP provides a set of ALV (ABAP List Viewer) & function modules, which can be used to enhance the readability and functionality of any report output.
    ALV is a flexible tool used for displaying lists.The tool provides common list functions & can be enhanced by self-defined options.
    so u will get the option for sorting in your alv report.
    Thanks

  • How to sort two different hierarchies in one dimension

    Does anyone know of a way to sort two different hierarchies in one dimension and still make drilling work correctly? We have two hierarchies in our item dimension; one called category and the other origin. Simplified, it looks like this:
    CATEGORY
    Hardware (100)
    ..PCs (100.100)
    ....PC 1
    ....PC 2
    ....PC 3
    ..Monitors (100.200)
    ....Monitor 1
    ....Monitor 2
    ....Monitor 3
    Software (200)
    ..Big (200.100)
    ....ERP package
    ....CRM package
    ..Small (200.200)
    ....Solitaire
    ....Mine Sweeper
    ORIGIN
    Vendor A (10)
    ..Site A1 (10.10)
    ....ERP package
    ..Site A2 (10.20)
    ....PC 1
    ....Monitor 3
    Vendor B (20)
    ..Site B1 (20.10)
    ....PC 2
    ....PC 3
    ....Monitor 1
    ....Monitor 2
    ....Solitaire
    ....Mine Sweeper
    ..Site B2 (20.20)
    ....CRM package
    We have numeric codes at each level above item which I represents the sort order (the number in parentheses at each level), and the items themselves should be sorted according to item number. I have implemented this (level code/item number) as an attribute in AWM making this the default sort order. However, as item is the lowest level in each hierarchy, I have only been able to list the items under the correct level in one hierarchy. As soon as I drill using the other hierarchy, the levels above item are sorted correctly, but the items appear at very odd places...
    The AWM documentation states that if default order is not selected on any attribute, hierarchies are sorted in the order they are created. Is there a way to control this order?
    Any input will be greatly appreciated!

    Hi,
    thank you for your answer. Yes, now I also find the class CL_SALV_WD_MULTI_CELL_EDITOR which could be used to set different UIE in one cell. But it is quite limited, just the following UIE could be used
    - LinkToAction 
    - LinkToURL    
    - FileDownload 
    - Button       
    - ToggleButton 
    best regards,
    Wenwen

  • How do I insert multiple values into different fields in a stored procedure

    I am writing a Stored Procedure where I select data from various queries, insert the results into a variable and then I insert the variables into final target table. This works fine when the queries return only one row. However I have some queries that return multiple rows and I am trying to insert them into different fields in the target table. My query is like
    SELECT DESCRIPTION, SUM(AMOUNT)
    INTO v_description, v_amount
    FROM SOURCE_TABLE
    GROUP BY DESCRIPTION;
    This returns values like
    Value A , 100
    Value B, 200
    Value C, 300
    The Target Table has fields for each of the above types e.g.
    VALUE_A, VALUE_B, VALUE_C
    I am inserting the data from a query like
    INSERT INTO TARGET_TABLE (VALUE_A, VALUE_B, VALUE_C)
    VALUES (...)
    How do I split out the values returned by the first query to insert into the Insert Statement? Or do I need to split the data in the statement that inserts into the variables?
    Thanks
    GB

    "Some of the amounts returned are negative so the MAX in the select statement returns 0 instead of the negative value. If I use MIN instead of MAX it returns the correct negative value. However I might not know when the amount is going to be positive or negative. Do you have any suggestions on how I can resolve this?"
    Perhaps something like this could be done in combination with the pivot queries above, although it seems cumbersome.
    SQL> with data as (
      2        select  0 a, 0 b,  0 c from dual   -- So column a has values {0, 1, 4},
      3  union select  1 a, 2 b, -3 c from dual   --    column b has values {0, 2, 5},
      4  union select  4 a, 5 b, -6 c from dual ) --    column c has values {0, -3, -6}.
      5  --
      6  select  ( case when max.a > 0 then max.a else min.a end) abs_max_a
      7  ,       ( case when max.b > 0 then max.b else min.b end) abs_max_b
      8  ,       ( case when max.c > 0 then max.c else min.c end) abs_max_c
      9  from    ( select  ( select max(a) from data ) a
    10            ,       ( select max(b) from data ) b
    11            ,       ( select max(c) from data ) c
    12            from      dual ) max
    13  ,       ( select  ( select min(a) from data ) a
    14            ,       ( select min(b) from data ) b
    15            ,       ( select min(c) from data ) c
    16            from      dual ) min
    17  /
    ABS_MAX_A  ABS_MAX_B  ABS_MAX_C
             4          5         -6
    SQL>

  • How to creat a Data provider  based on different fields in SAP BW ?

    Hi,
    Experts,
    There are  20 fields  of  Plant Maintainace  like : 
    SWERK
    BEBER
    STORT
    TPLNR
    EQUNR
    INGRP
    QMDAT   ---peroid
    STTXT
    QMDAT  - Date of Notification
    QMNUM
    QMTXT
    QMART
    AUSVN
    AUZTV
    AUSBS
    AUZTB
    AUSZT
    ERNAM
    QMDAB
    AUFNR
    I  want to creat a  Report based upon these fields  ?
    For that I h'v  checked the relevant Fields to the   existing standard  Datasource  in Bw side   &
    Checked  cubes   created  based upon these Datasource  in Bw side !
    i h'v found  some fields are  existing different cubes & some are  missing .
    How to creat a Data provider  based on different fields in SAP BW ?
    plz suggest      !!!!!!!
    Thanx,
    Asit
    Edited by: ASIT_SAP on Jul 15, 2011 6:25 AM
    Edited by: ASIT_SAP on Jul 15, 2011 6:27 AM
    Edited by: ASIT_SAP on Jul 15, 2011 12:37 PM

    Hi Lee, Please see below..
    DECLARE @Machine2 TABLE
    DispatchDate DATE
    INSERT INTO @Machine2 VALUES ('2014/02/01'), ('2014/02/02'), ('2014/02/03')
    DECLARE @DateFrom DATE
    SELECT @DateFrom = DATEADD(D,1,MAX(DispatchDate)) FROM @Machine2
    SELECT @DateFrom AS DateFrom
    Please mark as answer, if this has helped you solve the issue.
    Good Luck :) .. visit www.sqlsaga.com for more t-sql code snippets and BI related how to articles.

  • How to get different field in two are more different table using open sql

    Dear all,
              This SenthilMani am very new into sap abap am having doubt in reports how get the different fields from different tables like mara,marc,mard using open sql and native sql program give me some tips to get the data .
    with regards,
    senthil

    HI ,
      1) If u want to select data from more the two table then u can use FOR ALL ENTRIES.
             EX ..Open sql
                       select matnr from mara into table t_mara.
                      select matnr werks from marc into table t_marc for all entries in t_mara where matnr = t_mara-matnr.
      2) U can join more than one table.
               ex:
                   select mara~matnr
                              marc~werks
                    from mara join marc into table t_maramarc
                     on maramatnr = marcmatnr
    3) Using  native sql  ...only u can use JOIN statement

  • Is it possible to sort by multiple fields?

    Is it possible to sort the music library by multiple fields in a prioritised order? For example, I would like to sort by the following columns, in the following order:
    Album Artist
    Album
    Track Number
    for some reason I can't seem to do this, sorting by Album artist alone is not working for me, as some CD's I've imported have featured artists and this breaksup the listing order.
    Thanks

    Thanks Thomas
    Unfortunately, this solution did not work for me, cover flow still shows multiple instances of the same artwork for various groups of tracks from the same album.
    The thing I don't understand is why sorting by the "sort album artist" field does not group all the tracks from an album together.
    My specific example (I still can't get a screenshot posted):
    I have two Gwen Stefani albums, both ripped from CD (not imported from the itunes store). both albums have a majority of track where Gwen is the only artist and two or three track with featured artists. I have filled the "album artist" and "sort album artist" tags to all read Gwen Stefani, however, if I sort by either of these columns (in cover flow mode), what I get is this:
    all the tracks from one ablum where Gwen Stefani is the artist
    then all the tracks from the other album where Gwen Stefani is the artist
    then (in alphabetical order by featured artist) the remaining tracks from both albums - ie, the albums are mixed up
    for each group of track I see an cover flow art work.
    I should say that I have turned off the compilation tag on all of my albums because this ends up saving the file in a compilation folder, not in the artist folder on my hard drive - but it doesn't make a difference either way to the way I see the files sorted in itunes.
    It's really frustrating!

  • Formula that looks at two different fields

    Hi,
    I posted this request for help not too long ago but didn't clearly explain what is needed. 
    When text is entered into a field (currentgoal1, currentgoal2 - each equaling 1), the formula needs to look at a different field (point1, point 2 - each holds a list box of with points 1, 3, 5, or NA) to see if "NA" was chosen.  If NA was chosen (rather than a point, 1, 3, 5) then the goalcount (total number of populated currentgoal fields) needs to subtract 1 from goalcount.  
    currentgoal 1 = 1 point
    currentgoal 2 = 1 point
    currentgoal 3 = NA
    3 goals, 2 points, 1 point field with NA
    Since one of the current goals is has a point of NA, the overall count of currentgoals in the goalcount field (total number of populated currentgoal fields) should state 2 with the totalpoints field (all points fields added together) stating 2. 
    How can the formula be adjusted to subtract a currentgoal with a chosen point of NA? 
    Formula used now for counting current goals:
    goalcount = 0
    != null){
    = goalcount + 1;
    != null){
    = goalcount + 1;
    (Table3.Row3.currentgoal3.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row4.currentgoal4.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row5.currentgoal5.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row6.currentgoal6.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row7.currentgoal7.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row8.currentgoal8.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row9.currentgoal9.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row10.currentgoal10.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row11.currentgoal11.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row12.currentgoal12.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row13.currentgoal13.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row14.currentgoal14.rawValue!= null){
    = goalcount + 1;
    if(Table3.Row15.currentgoal15.rawValue!= null){
    = goalcount + 1;
    = goalcount;
    this.rawValuegoalcount
    goalcount
    goalcount
    goalcount
    goalcount
    goalcount
    goalcount
    goalcount
    goalcount
    goalcount
    goalcount
    goalcount
    goalcount
    if
    goalcount
    (Table3.Row2.currentgoal2.rawValue
    if
    goalcount
    (Table3.Row1.currentgoal1.rawValue
    if
    var

    Sorry, I was trying to edit. Is this better?
    var 
    goalcount = 0
    if
    (Table3.Row1.currentgoal1.rawValue
    != null){goalcount
    = goalcount + 1;}
     if(Table3.Row2.currentgoal2.rawValue!= null){goalcount
    = goalcount + 1;}
     if(Table3.Row3.currentgoal3.rawValue!= null){goalcount
    = goalcount + 1;}
    this.rawValue= goalcount;

  • Different field mode(display/change) in sales order payment terms (VA02)

    Hello All,
    In the VA02 transaction, when we are changing the sales orders, in the billing document of that material shows different modes for different fields.
    VA02->DOUBLE CLICK ON THE MATERIAL->BILLING DOCUMENT->FIELD PAYMENT TERMS.
    For some orders, field is in display mode and for some orders, it is in changeable mode.
    Could anyone please let me know about this..?
    thanks and regards,
    MERVIN

    Dear Jignesh
    As you would be aware Payment Terms will flow in SO both at header level and at item level.
    So in order to control the item level changes, go to VOV7, select the item category and untick the box [Business Item]   By doing so, whatever header level datas flows into SO, that will be copied to item level also and you will be controlling the changes at item level and end users cannot make changes at item level.
    Now you have to control at header level for which you have to apply User exits in the program MV45AFZZ - USEREXIT_MOVE_FIELD_TO_VBAK
    thanks
    G. Lakshmipathi

  • Is there a way to sort a PDF file with multiple pages by a specific field within the PDF? All pages have the same invoice format and i would like to sort by a field within and then print the documents. (Vendor, PO

    Is there a way to sort a PDF file with multiple pages by a specific field within the PDF? All pages have the same invoice format and i would like to sort by a field within and then print the documents. (Vendor, PO#, Date, ect.)

    When you say field, do you mean specifically a form field, such as a text field? Or is the information regular text on the page?

  • Collections.sort() - sort on multiple fields?

    I have a collection of objects in a List that I need to be able to sort on each field individually. For example, the data in the List is eventually displayed on a table in a web page. The user is able to click on a column header and the table is to be refreshed with the list contents sorted by that column (asc/desc).
    I would rather not re-query the DB for this list (it's pretty static and already saved in memory). So if I use the Collections.sort(myList, new Comparator() { ...} ) method, is it possible for me to pass the field/direction of the sort into the Comparator? Or, do I have to define individual Comparators for every field that the user can sort by and surround it all by an ugly switch statement?

    Honestly I think that a re-query to the DB is the best approach. Remember that any decent DB engine will cache queries so it will really only have to re-perform the sort algorithm.
    Commercial databases are very fast and almost never the source of a performance bottleneck.
    I realize this isn't the answer you want, but in my experience it is the best solution, if you really can't stand to re-query then as mentioned creating your own comparator is the way to go.

Maybe you are looking for

  • How to create transparent image at run-time?

    How to create transparent image at run-time? I mean I want to create a (new) transparent image1, then show other (loaded) transparent image2 to image1, then show image1 to a DC. The problem is - image1 has non-transparent background...

  • HP Proliante 320 g8 and Windows 7 64 bit

    I know that HP Proliante 320 g8 and HP Proliante 320 g8 v2 do not supported Windows 7, but our company already has bought these computers and license Windows 7. I can install Windows 7, but I do not have drivers. More precisely Windows wants drivers

  • Java.lang.NoClassDefFoundError in webdynpro application.

    Hi Experts I have developed an webdynpro java applicaton which uses an EJB .The application is working fine on my client machine,but when i try to migrate the project to other client machine and deploy the application it is showing a classpath error.

  • Creating new Managed Servers on remote machines

    Hi, We have a multi-machine domain already in place. Nodemanager is configured and we're using the architecture suggested in Oracle SOA Enterprise Deployment Guide, where we have one domain home for the AdminServer and one domain home for the Managed

  • How to change my nike account in iTunes ?

    It seems to be impossible to change my nike account in iTunes. The button "change my nike id" is now not in the iTunes ! So my data run are sent to nike website, but not in my account. it is a majors bug