CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 24
  1. #1
    Join Date
    Nov 2005
    Posts
    135

    remove duplicates and sort array

    I have a multi level array.
    I want to remove duplicates from the array and then sort it.
    Problem is I want to do this to different columns of the array at different times.
    So, first time I sort by workinstructure[i][0] and remove any duplicates in [i][0], next time I need to do the same but for column [i][3]

    Code:
    	//create copy of the array to work with	
    	var workingstructure = structure;
    
    	//initialise
    	if (document.getElementById) {
    	document.getElementById("Division").disabled=true;
    	document.getElementById("DivisionalLevels").disabled=true;
    	document.getElementById("BusUnitLevels").disabled=true;
    	document.getElementById("RegionalLevels").disabled=true;
    	document.getElementById("CostCentreLevels").disabled=true;
    	}
    
    	//sort the array, remove duplicates, etc.
    	workingstructure.sort; //i actually need to sort by different columns here, in this example it's [i][0] of the array
    	//for (i = 0; i < workingstructure.length; i++) {
    	//	if (workingstructure[i][0] == some duplicate check here) {
    	//		workingstructure.splice(i);
    	//	}
    	//}
    
    	//fill with data from the copy javascript array
    	for (i = 0; i < workingstructure.length; i++) {
    		var optn = document.createElement("OPTION");
    		optn.text = workingstructure[i][1];
    		optn.value = workingstructure[i][0];
    		document.getElementById("Division").options.add(optn);
    	}
    
    	//destroy the array
    	workingstructure = null;

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: remove duplicates and sort array

    What exactly is your problem, the removing of the duplicates? Or knowing which column is the next one?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Nov 2005
    Posts
    135

    Re: remove duplicates and sort array

    How to remove duplicates from the array.
    Example data:
    1,Jones, SY300
    2,SMith, SY300
    1,ABAB, Sy400

    Sometimes, I need to check column 1 and remove duplicates leaving me with:
    1,Jones, SY300
    2,SMith, SY300

    Sometimes, I need to check column 3 and remove duplicates leaving me with:
    1,ABAB, Sy400

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: remove duplicates and sort array

    To remove duplicates is easy. Take a look at an example from my website.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Nov 2005
    Posts
    135

    Re: remove duplicates and sort array

    but doesnt that look through the whole array?
    How do I tell it to delete duplicates from column 2 only ?
    See data example above

    Code:
    	//create copy of the array to work with	
    	var workingstructure = structure;
    
    	//initialise
    	if (document.getElementById) {
    	document.getElementById("Division").disabled=true;
    	document.getElementById("DivisionalLevels").disabled=true;
    	document.getElementById("BusUnitLevels").disabled=true;
    	document.getElementById("RegionalLevels").disabled=true;
    	document.getElementById("CostCentreLevels").disabled=true;
    	}
    
    
    	//fill with data from the copy javascript array
    	unique(workingstructure[1],false);
    
    	for (i = 0; i < structure.length; i++) {
    		var optn = document.createElement("OPTION");
    		optn.text = workingstructure[i][1];
    		optn.value = workingstructure[i][0];
    		document.getElementById("Division").options.add(optn);
    	}
    
    	//destroy the array
    	workingstructure = null;

  6. #6
    Join Date
    May 2002
    Posts
    10,943

    Re: remove duplicates and sort array

    Quote Originally Posted by JACKWEBS
    but doesnt that look through the whole array?
    Not a whole two-dimensional array, but it does an array. Just pass the second dimension to it.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Nov 2005
    Posts
    135

    Re: remove duplicates and sort array

    But how do I do that?
    If I pass array[2], it will think I'm talking about the 3rd row.
    If I pass [i][2], it won't know which row to use.
    Am I supposed to loop it? for i=0, i<array.length, i++
    then use your function every time?

  8. #8
    Join Date
    May 2002
    Posts
    10,943

    Re: remove duplicates and sort array

    You were close in your first try. Notice it doesn't clean the array, it returns a new array without the duplicates.

    Code:
    workingstructure[1] = unique(workingstructure[1], false);
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  9. #9
    Join Date
    Nov 2005
    Posts
    135

    Re: remove duplicates and sort array

    just says object doesn't support this property or method?
    Also, the FirefOx debugger says tempArray[ii].toLowerCase is not a function

    Code:
    	//create copy of the array to work with	
    	//var workingstructure = structure;
    	workingstructure = new Array;
    
    	//initialise
    	if (document.getElementById) {
    	document.getElementById("Division").disabled=true;
    	document.getElementById("DivisionalLevels").disabled=true;
    	document.getElementById("BusUnitLevels").disabled=true;
    	document.getElementById("RegionalLevels").disabled=true;
    	document.getElementById("CostCentreLevels").disabled=true;
    	}
    
    
    	//fill with data from the copy javascript array
    	//workingstructure[0] = unique(structure[0],false);
    	workingstructure[1] = unique(structure[1],false);
    
    	for (i = 0; i < structure.length; i++) {
    		var optn = document.createElement("OPTION");
    		optn.text = workingstructure[i][1];
    		optn.value = workingstructure[i][0];
    		document.getElementById("Division").options.add(optn);
    	}
    
    	//destroy the array
    	workingstructure = null;
    Last edited by JACKWEBS; January 23rd, 2008 at 04:36 AM.

  10. #10
    Join Date
    May 2002
    Posts
    10,943

    Re: remove duplicates and sort array

    The function toLowerCase() can fail if the variable is not a string. They only way we can truly help you is if you post the array as well. Can you please do so?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  11. #11
    Join Date
    Nov 2005
    Posts
    135

    Re: remove duplicates and sort array

    Aha.
    That will be why. The first element is a number, all the rest are text:

    array:
    0 3,Infrastructure,SY400,SY410,SY414,ZACY
    1 5,Regions,SY600,SY610,SY610,ZALY
    2 3,Infrastructure,SY400,SY410,SY410,ZAVI
    3 4,Management,SY300,SY340,SY344,ZBBC
    4 1,Central Services,SY700,SY700,SY700,ZBDX
    5 4,Management,SY300,SY340,SY345,ZBFP
    6 3,Infrastructure,SY400,SY440,SY440,ZBHX
    row7 etc.
    row 8 etc.

    can I just change the 1st element to text.
    SO just put in "3"
    ??
    Last edited by PeejAvery; January 23rd, 2008 at 01:55 PM. Reason: Merged two posts.

  12. #12
    Join Date
    May 2002
    Posts
    10,943

    Re: remove duplicates and sort array

    Am I to assume that the 0-6 are the indexes of the first dimension and that rest of each line delimited by the commas are the second dimensions?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  13. #13
    Join Date
    Nov 2005
    Posts
    135

    Re: remove duplicates and sort array

    Quote Originally Posted by PeejAvery
    Am I to assume that the 0-6 are the indexes of the first dimension and that rest of each line delimited by the commas are the second dimensions?
    Yes, sorry.
    So If you put in array[1][0] you get 5
    array [2][2] you get SY400

    the array[i][0] is put in as a number in javascript when I build the array, all the others are text as they have "" round them

  14. #14
    Join Date
    May 2002
    Posts
    10,943

    Re: remove duplicates and sort array

    All you needed to do was add the toString() function to the check.

    Code:
    <script type="text/javascript">
    function unique(theArray, caseSensitive){
      var inArray;
      tempArray = [];
      for(i = 0; i < theArray.length; i++){
        inArray = false;
        for(ii = 0; ii < tempArray.length; ii++){
          if(caseSensitive){
            if(tempArray[ii] == theArray[i]){inArray = true;}
          }
          else{
            if(tempArray[ii].toString().toLowerCase() == theArray[i].toString().toLowerCase()){inArray = true;}
          }
        }
        if(!inArray){tempArray[tempArray.length] = theArray[i];}
      }
      return tempArray;
    }
    
    var testArray = [
      [3, "Infrastructure", "SY400", "SY410", "SY414", "ZACY"],
      [5, "Regions", "SY600", "SY610", "SY610", "ZALY"],
      [3, "Infrastructure", "SY400", "SY410", "SY410", "ZAVI"],
      [4, "Management", "SY300", "SY340", "SY344", "ZBBC"],
      [1, "Central Services", "SY700", "SY700", "SY700", "ZBDX"],
      [4, "Management", "SY300", "SY340", "SY345", "ZBFP"],
      [3, "Infrastructure", "SY400", "SY440", "SY440", "ZBHX"]
    ];
    
    alert(testArray[2]);
    alert(unique(testArray[2]));
    </script>
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  15. #15
    Join Date
    Nov 2005
    Posts
    135

    Re: remove duplicates and sort array

    Something weird is definitely happening here.
    When I run it, it seems to only list 1 row, the number 3.
    Is that because I only use this line once?
    workingstructure[0] = unique(structure[0],false);

    For example, in the code above when you alert(TestArray[2]); it only alerts one row of the array. And shouldn't it pick out the value SY400? It is alreting the entire row.
    unique only seems to be returning 1 row. Can't figure out where though

    Code:
    	//create copy of the array to work with	
    	//var workingstructure = structure;	
    	var workingstructure = new Array();
    
    	//initialise
    	if (document.getElementById) {
    	document.getElementById("Division").disabled=true;
    	document.getElementById("DivisionalLevels").disabled=true;
    	document.getElementById("BusUnitLevels").disabled=true;
    	document.getElementById("RegionalLevels").disabled=true;
    	document.getElementById("CostCentreLevels").disabled=true;
    	}
    
    	workingstructure[0] = unique(structure[0],false);
    	//workingstructure[1] = unique(structure[1],false);	
    
    	for (i = 0; i < structure.length; i++) {
    		var optn = document.createElement("OPTION");
    		optn.text = workingstructure[i][0];
    		optn.value = workingstructure[i][0];
    		document.getElementById("Division").options.add(optn);
    	}
    
    	//destroy the array
    	workingstructure = null;
    Int his array:
    Code:
    var testArray = [
      [3, "Infrastructure", "SY400", "SY410", "SY414", "ZACY"],
      [5, "Regions", "SY600", "SY610", "SY610", "ZALY"],
      [3, "Infrastructure", "SY400", "SY410", "SY410", "ZAVI"],
      [4, "Management", "SY300", "SY340", "SY344", "ZBBC"],
      [1, "Central Services", "SY700", "SY700", "SY700", "ZBDX"],
      [4, "Management", "SY300", "SY340", "SY345", "ZBFP"],
      [3, "Infrastructure", "SY400", "SY440", "SY440", "ZBHX"]
    ];
    I would expect unique to return 3,5,4, and 1
    Yet it only returns the value 3 because it looks at the first row only when you put in array[0]
    Last edited by JACKWEBS; January 24th, 2008 at 05:42 AM.

Page 1 of 2 12 LastLast

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured