thefortrees Posted August 3, 2007 Share Posted August 3, 2007 Hi all - I need to implement some sorting algorithm. I have a webpage that has n number of questions. The user can reorder the questions as desired with drop down menus. Description of problem: The questions are in position as follows: q1->1, q2->2, q3->3, q4->4, q5->5 If I change the position with the drop down menus, I could possibly have: q1->1, q2->5, q3->3, q4->4, q5->5 Which, upon form submission would have to be reordered before being stored in the database to this: q5->1, q1->2, q3->3, q4->4, q2->5 Any suggestions on where/how to start? Quote Link to comment https://forums.phpfreaks.com/topic/63233-selection-sort/ Share on other sites More sharing options...
simcoweb Posted August 3, 2007 Share Posted August 3, 2007 Question: Even though they may change order, the question id's or field names don't change. Correct? Quote Link to comment https://forums.phpfreaks.com/topic/63233-selection-sort/#findComment-315162 Share on other sites More sharing options...
hasanatkazmi Posted August 3, 2007 Share Posted August 3, 2007 i think you havnt thougt well, nothing will be changed at server, Quote Link to comment https://forums.phpfreaks.com/topic/63233-selection-sort/#findComment-315174 Share on other sites More sharing options...
Psycho Posted August 3, 2007 Share Posted August 3, 2007 I have typically used Javascript to do the renumbering before submitting for processing, but ideally you should also validate on the srver side. Your example is pretty simplistic. But, without some validation on the client-side, you are going to have some issues to deal with. What if the user was to set all the questions to #5, what then? Or if you moved item two to positin 3 and item three to position 4. Would you move item four to position 2? The easiest approach in my opinion would be to simply order the options by the new sort and then do a secondary sort by the original order in reverse order. Here is something I started working on this week that may help (only works on IE at the moment). But it may be a better way of allowing the user to reorder the questions: <html> <head> <script type="text/javascript"> //==================================================== function addOption (fieldObj, valueStr, textStr, index, selected) { //==================================================== var index = (index!==false)?index:fieldObj.length; var textStr = (textStr!==false)?textStr:valueStr; var selected = (selected==true)?true:false; var newOption = document.createElement('option'); newOption.text = textStr; newOption.value = valueStr; newOption.selected = selected; fieldObj.add(newOption, index); } //==================================================== function deleteOption (fieldObj, optIndex) { //==================================================== if(fieldObj.options[optIndex]) { fieldObj.options[optIndex] = null; } } //==================================================== function orderOptions(fieldID, action) { //==================================================== var fieldObj = document.getElementById(fieldID); if (action=='down' || action=='bottom') { var startIdx = fieldObj.length-1; var endIdx = -1; var step = -1; if (action=='bottom') { var newIdx = fieldObj.length-1; } else { //action = 'down' for (var i=startIdx; i>=fieldObj.selectedIndex; i--) { if (fieldObj.options[i].selected) { var newIdx=i+1; break;} } newIdx = (newIdx>fieldObj.length-1)?fieldObj.length-1:newIdx; } } else { //action = 'up' or 'top' var startIdx = 0; var endIdx = fieldObj.length; var step = 1; if (action=='top') { var newIdx = 0; } else { // Action='up' : default var newIdx = (fieldObj.selectedIndex-1); newIdx = (newIdx<0)?0:newIdx; } } for (var optIdx=startIdx; optIdx!=endIdx; optIdx+=step) { if (fieldObj.options[optIdx].selected) { //Save current values var optSaveText = fieldObj.options[optIdx].text; var optSaveValue = fieldObj.options[optIdx].value; //Delete and recreate the option at new index deleteOption (fieldObj, optIdx); addOption (fieldObj, optSaveValue, optSaveText, newIdx, true); //Adjust index value newIdx += step; } //End if } //End for loop return; } //==================================================== function sortOptions(fieldID, direction) { //==================================================== var fieldObj = document.getElementById(fieldID); //Create an array of the option properties optionAry = new Array(); for (var optIdx=0; optIdx<fieldObj.options.length; optIdx++) { op = fieldObj.options[optIdx]; optionAry[optIdx] = new Array(); optionAry[optIdx]['text'] = op.text; optionAry[optIdx]['value'] = op.value; optionAry[optIdx]['selected'] = op.selected; } //Sort the array optionAry.sort(sortOptionArray); if (direction=='desc') { optionAry.reverse(); } //Repopulate the option list for (var optIdx=0; optIdx<fieldObj.options.length; optIdx++) { fieldObj.options[optIdx].text = optionAry[optIdx]['text']; fieldObj.options[optIdx].value = optionAry[optIdx]['value']; fieldObj.options[optIdx].selected = optionAry[optIdx]['selected']; } return; } //==================================================== function sortOptionArray (a, b) { var aText = a['text'].toLowerCase(); var bText = b['text'].toLowerCase(); if (aText < bText) { return -1; } if (aText > bText) { return 1; } return 0; } function populateFields() { // } </script> </head> <body> <span style="border:0px solid red;width:220px;text-align:left;vertical-align:middle;"> Assigned:<br> <select name="assigned" id="assigned" size="10" style="width:220px;" multiple onChange=""> <option value="Question 1">Question 1</option> <option value="Question 2">Question 2</option> <option value="Question 3">Question 3</option> <option value="Question 4">Question 4</option> <option value="Question 5">Question 5</option> </select> </span> <span style="border:0px solid red;width:70px;text-align:middle;vertical-align:middle;"><br> <button id="moveTop" style="width:50px;font-size:8pt;" onclick="orderOptions('assigned', 'top');" title="Move to top">Top</button><br> <button id="moveUp" style="width:50px;font-size:8pt;" onclick="orderOptions('assigned', 'up');" title="Move up">Up</button><br> <button id="moveDown" style="width:50px;font-size:8pt;" onclick="orderOptions('assigned', 'down');" title="Move down">Down</button><br> <button id="moveBottom" style="width:50px;font-size:8pt;" onclick="orderOptions('assigned', 'bottom');" title="Move to bottom">Bottom</button><br> <br> <button id="sortDesc" style="width:50px;font-size:8pt;" onclick="sortOptions('assigned', 'asc');" title="Sort ascending">A-Z</button><br> <button id="sortAsc" style="width:50px;font-size:8pt;" onclick="sortOptions('assigned', 'desc');" title="Sort descending">Z-A</button> </span> </body> </htmL> Quote Link to comment https://forums.phpfreaks.com/topic/63233-selection-sort/#findComment-315184 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.