Qsorb
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.
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>
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
150
350
Equally?
350
Equally?
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 :))
ASKER
Thanks guys for the quick and excellent work and no confusion.