How to sort div container using javascript

using jquery how can I dynamically update the data-index value and rearrange the div container.  

<div>
    <div id="divA" data-index="1"></div>
    <div id="divB" data-index="3"></div>
    <div id="divC" data-index="2"></div>
</div>

Open in new window


loking for function as
function sort(a){
     if a = 'ByProd' {
                divA.data-index=1;
                divB.data-index=3;
                divC.data-index=2;
   }else if a = 'ByProd' {
                divA.data-index=3;
                divB.data-index=1;
                divC.data-index=2;
 }else{
                divA.data-index=1;
                divB.data-index=2;
                divC.data-index=3;
}

}

Open in new window

Bharat GuruAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

leakim971PluritechnicianCommented:
test page : https://jsfiddle.net/7910asdo/
function sort(a){
	switch(a) {
     	case "ByProd": 
      	$("#divA").data("index", 1);
      	$("#divB").data("index", 3);
      	$("#divC").data("index", 2);
        break;
			case "BySome":
      	$("#divA").data("index", 3);
      	$("#divB").data("index", 1);
      	$("#divC").data("index", 2);
        break;
			default:
      	$("#divA").data("index", 1);
      	$("#divB").data("index", 2);
      	$("#divC").data("index", 3);
	}
}

Open in new window

0
Bharat GuruAuthor Commented:
how to do same using javascript
0
leakim971PluritechnicianCommented:
why did you add jQuery topics on your question if you want only pure Javascript answer?
0
leakim971PluritechnicianCommented:
        function sort(a){
            var divA = document.getElementById("divA");
            var divB = document.getElementById("divB");
            var divC = document.getElementById("divC");
            switch(a) {
                case "ByProd":
                    divA.dataset.index =  1;
                    divB.dataset.index =  3;
                    divC.dataset.index =  2;
                    break;
                case "BySome":
                    divA.dataset.index =  3;
                    divB.dataset.index =  1;
                    divC.dataset.index =  2;
                    break;
                default:
                    divA.dataset.index =  1;
                    divB.dataset.index =  2;
                    divC.dataset.index =  3;
            }
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.