Link to home
Start Free TrialLog in
Avatar of Qsorb
QsorbFlag for United States of America

asked on

Sort JavaScript output

This code works great, thanks to Slick812. But I need the output sorted by the number of times the duplicate string is found, in descending order.

Also prefer to list just the first 10 matches but that is not as important as the sort.
<!DOCTYPE html>
<html>
<head>
  
  <style type='text/css'>
		
	}
  body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
	color: #000000;
}
body {
	background-color: #FFFFFF;
}
</style>

</head>
<body>




<script>
var tx1 = "Bobob is attempting-to find him the reason, Janet is attempting. to understand Bobob and to! reason with him a!s Bobob in reason or reason.";

function getDups(txt) { // Only has duplicates
// I had to remove most common punctuation
// BUT not every one, so BeWare if the text has other punctuation
txt=txt.replace(/,/g,"");
txt=txt.replace(/\./g,"");
txt=txt.replace(/\?/g,"");
txt=txt.replace(/!/g,"");
txt=txt.replace(/\"/g,"");
txt=txt.replace(/\'/g,"");
txt=txt.replace(/\-/g," ");
txt=txt.replace(/;/g,"");
txt=txt.replace(/:/g," ");

wordary = txt.split(' ');
wordary = wordary.sort();

var dNum = 1;
var dups = {};
for (var i=0; i < wordary.length-2; i++) {
if ( wordary[i].length > 4 
    && wordary[i+1].length > 4 
    && wordary[i+1] == wordary[i]) {
	++dNum;
	dups[wordary[i]] = dNum;
	} else dNum = 1;
}

txt = "";
var key = "";
for (key in dups) {
    txt+= key +" "+dups[key]+"<br />";
    }
return txt;
}


function showUm(text) {
text = getDups(text);
document.getElementById("de").innerHTML = text;
}

</script> 


<button onClick="showUm(tx1)"> Get Duplicates </button>
<p id="de"> </p> 

  
</body>
</html> 

Open in new window

SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Qsorb

ASKER

leakim971 was first so he get's half the points because Slick812 gave me both, he also set the amount of the results shown to ten, so I'm awarding points equally.

Thanks guys for the quick and excellent work and no confusion.
150
350

Equally?
Avatar of Qsorb

ASKER

Sorry, that should have been equally. Not sure what happened.  How do I ask the moderator or whomever controls the points to divvy them equally?
no, of course :))