PDA

View Full Version : How to sort text´s Array?


LottaLava
11-12-2002, 06:58 PM
Hello folks, is a question that can get out of my mind, how it could be?

How i could implement a sort( ) function for an Array objet to sort text?

Some idea?


LottaLava

beetle
11-12-2002, 07:06 PM
Is this an associative array and you need to sort it by key? Or do you want to sort it by value?

beetle
11-12-2002, 07:27 PM
Well, I'll guess you need just sorting by value. Javascript has a built in sort() method for arrays. By default, it lexicographically (0-9a-z) sorts the array by value. However, you can pass it a function pointer to a comparison function that automatically receives two parameters (I used a and b) These parameters represent two values being compared for sorting. Comparison functions should return a -1 if a is to be sorted before b, a 1 if b is to be sorted before a, and a 0 if both a and b are identical. Here's an example...<html>
<head>
<title>Array sorting</title>

<script>
var arr = ['banana','pineapple','apple','strawberry','orange'];

function reverseAlphabet(a, b) {
if (a > b) return -1;
if (a < b) return 1;
return 0;
}

function lastLetter(a, b) {
if (a.substring(a.length-1) < b.substring(b.length-1)) return -1;
if (a.substring(a.length-1) > b.substring(b.length-1)) return 1;
return 0;
}

</script>

</head>

<body>
<h3>Unchanged</h3>
<script>
for (var i in arr)
document.write(arr[i] + "<br>");
</script>

<h3>Alphabetical</h3>
<script>
arr.sort();
for (var i in arr)
document.write(arr[i] + "<br>");
</script>

<h3>Reverse Alphabetical</h3>
<script>
arr.sort(reverseAlphabet);
for (var i in arr)
document.write(arr[i] + "<br>");
</script>

<h3>Alphabetically by last letter</h3>
<script>
arr.sort(lastLetter);
for (var i in arr)
document.write(arr[i] + "<br>");
</script>

</body>
</html>

LottaLava
11-12-2002, 08:10 PM
Thanks beetle, I can believe it so clear now, here the whole code that i made with your example:

<html>
<head>
<script language="JavaScript">
var segmento = new TChildGroup
function teste( )
{
var frm = document.frmMain;

var segmento = new TChildGroup( "10", "Blah!" );

segmento.add( new TChildGroup( "452", "N" ));
segmento.add( new TChildGroup( "452", "Q" ));
segmento.add( new TChildGroup( "452", "L" ));
segmento.add( new TChildGroup( "452", "D" ));
segmento.add( new TChildGroup( "452", "Y" ));
segmento.add( new TChildGroup( "452", "E" ));
segmento.add( new TChildGroup( "452", "F" ));
segmento.add( new TChildGroup( "452", "U" ));
segmento.add( new TChildGroup( "452", "A" ));

segmento.sort( );

while ( segmento.next( ))
alert( segmento.item( ).descricao );
}
//---------------------------------------------------------------------
function sortChildren( )
{
this.aChildren.sort( sortObject );
}
//---------------------------------------------------------------------
function sortObject( objA_, objB_ )
{
if ( objA_.descricao == objB_.descricao )
return 0;
else
return ( objA_.descricao > objB_.descricao ) ? 1 : -1;
}
//---------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////
//---------------------------------------------------------------------
function TChildGroup( id_, descricao_ )
{
this.pos = -1;
this.aChildren = new Array( );

this.id = id_;
this.descricao = descricao_;

this.add = addChild;
this.next = nextChild;
this.prior = priorChild;
this.length = lengthChild;
this.item = itemChild;

this.owner = "";

this.sort = sortChildren;
}
//---------------------------------------------------------------------
function addChild( childGroup_ )
{
if ( childGroup_ != null )
{
if ( this.aChildren.length == 0 )
{
this.aChildren = new Array( );
this.aChildren[ 0 ] = childGroup_;
}
else
this.aChildren[ this.aChildren.length ] = childGroup_;
}
}
//---------------------------------------------------------------------
function nextChild( )
{
if ( this.pos < this.aChildren.length - 1 )
{
this.pos++;
return true;
}
else
return false;

//return this.aChildren[ this.pos++ ];
}
//---------------------------------------------------------------------
function priorChild( )
{
return this.aChildren[ this.pos-- ];
}
//---------------------------------------------------------------------
function lengthChild( )
{
return this.aChildren.length;
}
//---------------------------------------------------------------------
function itemChild( )
{
return this.aChildren[ this.pos ];
}
//---------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////
</script>
</head>
<body onLoad="teste( );">
<form name="frmMain"
method="POST"
action="/exec/PF_Blah">
<select name="cb">
<option value="7845" owner="hulk">Quake</option>
</select>
</form>
</body>
</html>