Results 1 to 2 of 2

Thread: Sorting Array

  1. #1
    saqib15 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default Sorting Array

    Any can do me favour i need to sort my Array i have array of users and array of roles i need to sort users by roles

    at line 122

    : I am checking to see if you need to add the role to your array

    : i just need to also add that user to another array to track the users in that particular role

    : like how you built getUserRoles the first time

    My java File is::::

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.idiominc.ws.integration.lsaweb;
    
    import com.idiominc.wssdk.WSContext;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import org.apache.log4j.Category;
    import com.idiominc.wssdk.component.servlet.WSHttpServlet;
    import com.idiominc.wssdk.user.WSLocale;
    import com.idiominc.wssdk.user.WSRole;
    import com.idiominc.wssdk.user.WSUser;
    import com.idiominc.wssdk.user.WSWorkgroup;
    import com.idiominc.wssdk.workflow.WSProject;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Iterator;
    
    /**
     *
     * @author sshaikh
     */
    public class ShowAssignees extends WSHttpServlet {
    
        private final Category log = Category.getInstance(this.getClass().getName());
    
        public boolean handle(WSContext ws, HttpServletRequest req, HttpServletResponse res) {
    
            try {
                PrintWriter out = res.getWriter();
    
                String proj_id = req.getParameter("proj_id");
                out.println(proj_id + "<br>");// For Test If Getting project id
                WSUser[] users = getAvailableUsers(ws, proj_id);
    
    
                for (int i = 0; i < users.length; i++) {
                    out.write("Users Are For Project--------" +users[i].getFullName() + "<br>");
                }
                
    
                WSRole[] roles = getUserRoles(users);
                for(int r=0; r<roles.length; r ++){
                   WSUser[] usergroup = roles[r].getUsers();
                   for(int g=0; g<usergroup.length; g++){
                     String ug = usergroup[g].getName();
                          out.println("Role are---------<ul><li>" + roles[r].getName()+"</ul></li>"+"<br>"+ug+"<br>");
    
                   
                }
                }
            } catch (java.io.IOException e) {
                log.error("WRITING SOMETHING");
            }
            return true;
    
        }
    
        // RETURNS ALL AVAILABLE USERS FOR THE GIVEN PROJECT ID
        private WSUser[] getAvailableUsers(WSContext ws, String proj_id) {
    
            // Create a user array -- set it to empty.
            WSUser[] users = null;
          
            // Fetch the project that corresponds to the given ID#
            int proid = Integer.parseInt(proj_id);//
    
            WSProject proj = ws.getWorkflowManager().getProject(proid);//fetching project
           
            //Get the target locales for this project
            WSLocale targetlocale = proj.getTargetLocale();
    
            // Get the workgroup that can work on this project
             WSWorkgroup workgroup = proj.getProjectGroup().getWorkgroup();
           
            // Get the list of users in that workgroup
                WSUser[] user = workgroup.getUsers();
               // WSUser[] outusers = new WSUser
                ArrayList list = new ArrayList();
                for(int k =0; k<user.length; k++){
            //Get The locales of this workgroup users
                  WSLocale[] locales  = user[k].getLocales();
                  for(int m=0; m<locales.length; m++){
    
            // For each user, compare his locale list to the target locale of the project and
           //add this to array list, tree map, concatenated string, etc
                     if(locales[m].equals(targetlocale)){
                        
                        list.add(user[k]);
                     }
                  }
                  
            }
            // If locale is OK, then add him to the output list.
                users = new WSUser[list.size()];//Converting List to arrray
                for(int l =0; l<list.size(); l++){
                    users[l]=(WSUser)list.get(l);//casting object to WSUser
                    WSWorkgroup[]wg = users[l].getWorkgroups();
                    for(int w=0; w<wg.length; w++){
                        String groupname= wg[w].getName();
                    }
                }
    
            return users;
        }
    
        // RETURNS ALL ROLES THAT THE LIST OF USERS IS IN
        private WSRole[] getUserRoles(WSUser[] users) {
            //Initialize an array of Roles -- set it to empty
            WSRole[] role = null;
            ArrayList list = new ArrayList();
            //Set set = new TreeSet();//if WSRole doenst have equal method then coparasion is required to remove dublicate.
            //For each user, get list of roles
            for(int i=0; i<users.length; i++){
            WSRole[] roles = users[i].getRoles();
    
            for(int r =0; r<roles.length; r++){
                boolean found =false;//initilizing boolean
                //---------------------------------------
               
                //---------------------------------------
               for(int s =0; s<list.size(); s++){
                   WSRole ro = (WSRole)list.get(s);
                   if(ro.getName().equals(roles[r].getName()))//Matching the dublicates
                   {
                       users[i].getWorkgroups();
                       found = true;//if found set true
                   }
               }
                if(!found)list.add(roles[r]);//if not found add into list
            }
            }
            role = new WSRole[list.size()];//converting list an array
            Iterator it = list.iterator();
            int count =0;
            while(it.hasNext()){
                role[count] =(WSRole)it.next();
                count++;
            }
    
            //Compare each role to the list; if it's not in the list yet, add it.
    
            return role ;
        }
    
        public String getName() {
            return "Show_Assignees";
        }
    
        public String getDescription() {
            return "Shows assignees for given proj_id";
        }
    
        public String getVersion() {
            return "1.0";
        }
    }
    Last edited by Eranga; 02-12-2010 at 03:43 AM. Reason: Added code tags.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Please use the code tags when you posting a code next time here in the forum. Unformated codes are really hard to read.

    Regarding your question, read more about java.util.Arrays.sort(); on Java API.

Similar Threads

  1. Sorting an Array via Stacks
    By viperlasson in forum New To Java
    Replies: 0
    Last Post: 02-01-2010, 06:53 AM
  2. HELP!!! Problems with Array: matching, sorting, etc
    By jrelvi23 in forum New To Java
    Replies: 15
    Last Post: 11-18-2009, 11:14 PM
  3. Converting array to list and sorting it
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:36 PM
  4. Sorting an array of Strings
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:39 PM
  5. Replies: 0
    Last Post: 04-14-2008, 08:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •