Thursday, May 7, 2026
HomeLanguagesJavascriptD3.js selection.order() Function

D3.js selection.order() Function

The selection.order() function is used to insert the selection again such that the order of each group matches the selection order.

Syntax:

selection.order();

Parameters: This function does not accept any parameters.

Return Values: This function does not return anything.

Example 1: Using selection.clone() method without using selection.order() method.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent="width=device-width,
                    initial-scale=1.0">
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
 
    <style>
        div {
            width: 300px;
            color: #ffffff;
            height: auto;
            background-color: green;
            margin: 10px;
        }
    </style>
</head>
 
<body>
    <h1 style="color:green;">
        neveropen
    </h1>
 
     
<p>D3.js selection.order() Function</p>
 
 
    <div><span>1. some text.</span></div>
    <div><span>2. some text.</span></div>
    <div><span>3. some text.</span></div>
    <button class="btn">click Here!</button>
 
    <script>
        function func1() {
 
            // Selecting div and Cloning the
            // divs
            // Inserting them just after the
            // element But no arranging in
            // original order.
            var div = d3.selectAll("div")
                        .clone(true)
        }
        btn = document.querySelector(".btn");
        btn.addEventListener("click", func1);
 
    </script>
</body>
 
</html>


Output:

  • Before clicking the button:

  • After clicking the button: Please note that elements are not inserted in the same order as they were before i.e 1 is inserted just after 1 and 2 is inserted just after 2 and so on.

Example 2: Using selection.clone() with selection.order() method.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" path1tent="width=device-width,
                    initial-scale=1.0">
    <script src="https://d3js.org/d3.v4.min.js">
    </script>
 
    <style>
        div {
            width: 300px;
            color: #ffffff;
            height: auto;
            background-color: green;
            margin: 10px;
        }
    </style>
</head>
 
<body>
    <h1 style="color:green;">
        neveropen
    </h1>
 
     
<p>D3.js d3.selection.order() Function</p>
 
 
    <div><span>1. some text.</span></div>
    <div><span>2. some text.</span></div>
    <div><span>3. some text.</span></div>
    <button class="btn">click Here!</button>
 
    <script>
        function func1() {
            // Selecting  div and
            // Cloning the divs
            // But also arranging in original order.
            var div = d3.selectAll("div")
                .clone(true)
                .order();
        }
        btn = document.querySelector(".btn");
        btn.addEventListener("click", func1);
 
    </script>
</body>
 
</html>


Output:

  • Before clicking the button:

  • After clicking the button: Please note that elements are exactly in the same order as they were before i.e 1, 2, 3, and 1, 2, 3.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32514 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6892 POSTS0 COMMENTS
Nicole Veronica
12012 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12106 POSTS0 COMMENTS
Shaida Kate Naidoo
7016 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6975 POSTS0 COMMENTS
Umr Jansen
6962 POSTS0 COMMENTS