javascript re order an array based on a contained arrays value

data = "val1;val2;val3;val4;val5;val6;val7;val8;val9,val1;val2;val3;val4;val5;val6;val7;val8;val9,val1;val2;val3;val4;val5;val6;val7;val8;val9;val1;val2;val3;val4;val5;val6;val7;val8;val9"

arrData = data.split(",")

arrData = reorder(arrData,7)

How can I re-order arrData so that the array is ordered by val8, the 8th value in the string?
KeithMcElroyAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Sar1973Connect With a Mentor Commented:
I think then that you need a function with a loop that replaces val8 with val8a, val8b, etc. with the fromCharCode function (see http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes for the codes).
0
 
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:
Hi,

arrData.sort(); will do the work.
0
 
KeithMcElroyAuthor Commented:
Actually, the 8th and then the 2nd  elements
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
KeithMcElroyAuthor Commented:
How do I get it to sort based on the 8th then the 2nd value of the semicolon parsed string within each array element?
0
 
KeithMcElroyAuthor Commented:
There are two delimiters, first a "," for the array, then semi-colon for each element within each array element.
0
 
Pierre CorneliusCommented:
I put together an example showing what you want. It displays the array in a table and if you click on the table headings, it will do the sort.
<!DOCTYPE HTML>
<html>
<head></head>
<body>

	<div id="example"></div>
	
	<script type="text/javascript">
	
	var data="apple;fruit;4,banana;fruit;2,peas;vegetables;8,carrots;vegetables;3";

	var arrData = data.split(',');
	var numRows = arrData.length;
	for (i in arrData)
	{
		arrData[i] = arrData[i].split(';');
	}
	var numCols = arrData[0].length;

	//put in table to illustrate
	function arrDataToTable()
	{
		var htmRows='';
		for (var row=0; row<numRows; row++)
		{
		  htmRows+='<tr>';	
		  for (var col=0; col<numCols; col++)
		  {
			htmRows+='<td>'+arrData[row][col]+'</td>'
		  }
		  htmRows+='</tr>';	
		}

		document.getElementById('example').innerHTML='<table border="1" style="border-collapse:collapse;"><thead><tr><th>Item</th><th>Type</th><th>Qty</th></tr></thead><tbody>'+htmRows+'</tbody></table>';
		
		//add heading click events
		var headings = document.getElementsByTagName("th"); 

		for (var i = 0; i < headings.length; i++ ) 
		{    
			headings[i].addEventListener("click", function() 
				{ 
				SortByCol(this.cellIndex); 
				}
				, false
				); 
		} 		
	}
	
	//sort function
	function SortByCol(col)
	{
		arrData.sort(function(a,b)
			{			
				//number sort for 3rd col
				if (col===2)
				{
					return (parseInt(a[col])===parseInt(b[col]))?0:(parseInt(a[col])>parseInt(b[col]))?1:-1;
				}
				else
				{
					//sort as text
					return (a[col]===b[col])?0:(a[col]>b[col])?1:-1;
				}
			}
		);
		arrDataToTable();
	}
	
	//create table using arrData to illustrate
	arrDataToTable();
	
	
	
	</script>

</body>
</html>

Open in new window

0
 
Sar1973Commented:
Are you looking for a code that reorders the elements by shifting the val8 at first position?
0
 
leakim971PluritechnicianCommented:
Test page : http://jsfiddle.net/6A8Bb/2/

var strData = "val1;val2;val3;val4;val5;val6;val7;val8;val9,val1;val2;val3;val4;val5;val6;val7;val8;val9,val1;val2;val3;val4;val5;val6;val7;val8;val9;val1;val2;val3;val4;val5;val6;val7;val8;val9"

var reorder = function(str) {
    var arrData = str.split(",");
    for(var i=0;i<arrData.length;i++) {
        var arr = arrData[i].split(";");
        arr = arr.sort(function(a, b) {
            return (a>b)?1:-1;
        });
        arrData[i] = arr.join(";");
    }
    return arrData.join(",");
}

strData = reorder(strData);

// CHECKING
alert(strData);

Open in new window

0
 
KeithMcElroyAuthor Commented:
I think this is close:  I modified the val8 to be 'out of order'.
the returned array would resort the semicolon delimited array into val8a,val8b,val8c.



var strData = "val1;val2;val3;val4;val5;val6;val7;val8b;val9,val1;val2;val3;val4;val5;val6;val7;val8c;val9,val1;val2;val3;val4;val5;val6;val7;val8;val9;val1;val2;val3;val4;val5;val6;val7;val8a;val9"
0
 
KeithMcElroyAuthor Commented:
The array would stay in it's original state of val1;val2,etc. in other words val8 would not move to the beginning.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.