Zum Inhalt springen

Need help making a 3 variable sorting script

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

💡 Struggling with complex sorting algorithms? Need help with multi-dimensional data processing? 🚀 Get Expert Help

CS

CodeSorter92

Posted on July 18, 2025 • Advanced

🔢 Multi-dimensional sorting challenge

Hey everyone! I’m working on a complex project that needs to sort 3D coordinates by multiple criteria. The sorting priority should be:

  1. Lowest Y coordinate first
  2. Within same Y, lowest X coordinate first
  3. Within same X, lowest Z coordinate first

I’ve tried several approaches but they either don’t sort properly or the algorithm behaves inconsistently depending on input order. Most online resources only cover single-variable sorting, which doesn’t help with this multi-criteria problem.

This is definitely pushing my data processing skills to the limit! Any guidance would be much appreciated! 🤯

AL

AlgorithmExpert

Replied 32 minutes later • ⭐ Best Answer

Great question @CodeSorter92! Multi-dimensional sorting is definitely a step up from basic algorithms. The key is to create a proper comparison function that handles all three criteria in the right priority order.

🧠 Understanding Multi-Dimensional Sorting

Here’s how the sorting logic works:

flowchart TD A[🔄 Compare Two Points] --> B{Y1 = Y2?} B -->|No| C[Compare Y Values] B -->|Yes| D{X1 = X2?} C --> E[Return Y1 < Y2] D -->|No| F[Compare X Values] D -->|Yes| G[Compare Z Values] F --> H[Return X1 < X2] G --> I[Return Z1 < Z2] E --> J[✅ Comparison Result] H --> J I --> J style A fill:#e1f5fe style J fill:#e8f5e8 style C fill:#fff3e0 style F fill:#fff3e0 style G fill:#fff3e0

🔧 Step 1: Create the Comparison Function

First, create a custom block that compares two 3D points:

    define compare point (x1) (y1) (z1) with (x2) (y2) (z2)
// Returns true if first point should come before second point
if <(y1) = (y2)> then
if <(x1) = (x2)> then
set [comparison result v] to <(z1) < (z2)>
else
set [comparison result v] to <(x1) < (x2)>
end
else
set [comparison result v] to <(y1) < (y2)>
end
  

📊 Step 2: Bubble Sort Implementation

Here’s a complete bubble sort using your comparison function:

    define sort 3D points
set [swapped v] to [true]
repeat until <not <(swapped)>>
set [swapped v] to [false]
set [i v] to [1]
repeat ((length of [X List v]) - (1))
// Get current and next points
set [x1 v] to (item (i) of [X List v])
set [y1 v] to (item (i) of [Y List v])
set [z1 v] to (item (i) of [Z List v])

set [x2 v] to (item ((i) + (1)) of [X List v])
set [y2 v] to (item ((i) + (1)) of [Y List v])
set [z2 v] to (item ((i) + (1)) of [Z List v])

// Compare points
compare point (x1) (y1) (z1) with (x2) (y2) (z2)

// Swap if needed
if <not <(comparison result)>> then
// Swap X coordinates
replace item (i) of [X List v] with (x2)
replace item ((i) + (1)) of [X List v] with (x1)

// Swap Y coordinates
replace item (i) of [Y List v] with (y2)
replace item ((i) + (1)) of [Y List v] with (y1)

// Swap Z coordinates
replace item (i) of [Z List v] with (z2)
replace item ((i) + (1)) of [Z List v] with (z1)

set [swapped v] to [true]
end

change [i v] by (1)
end
end
  

🚀 Step 3: More Efficient Quick Sort Version

For better performance with larger datasets:

    define quicksort 3D (low) (high)
if <(low) < (high)> then
partition 3D points from (low) to (high)
quicksort 3D (low) ((partition index) - (1))
quicksort 3D ((partition index) + (1)) (high)
end

define partition 3D points from (low) to (high)
// Use last element as pivot
set [pivot x v] to (item (high) of [X List v])
set [pivot y v] to (item (high) of [Y List v])
set [pivot z v] to (item (high) of [Z List v])

set [i v] to ((low) - (1))
set [j v] to (low)

repeat ((high) - (low))
set [curr x v] to (item (j) of [X List v])
set [curr y v] to (item (j) of [Y List v])
set [curr z v] to (item (j) of [Z List v])

compare point (curr x) (curr y) (curr z) with (pivot x) (pivot y) (pivot z)

if <(comparison result)> then
change [i v] by (1)
swap elements at (i) and (j)
end

change [j v] by (1)
end

change [i v] by (1)
swap elements at (i) and (high)
set [partition index v] to (i)
  

🎯 Step 4: Usage Example

Here’s how to use the sorting system:

    when flag clicked
// Initialize your coordinate lists
delete [all v] of [X List v]
delete [all v] of [Y List v]
delete [all v] of [Z List v]

// Add some test data
add [5] to [X List v]
add [3] to [Y List v]
add [8] to [Z List v]

add [2] to [X List v]
add [3] to [Y List v]
add [1] to [Z List v]

add [7] to [X List v]
add [1] to [Y List v]
add [4] to [Z List v]

// Sort the points
sort 3D points

// Display results
set [i v] to [1]
repeat (length of [X List v])
say (join (join (join (join [Point ] (i)) [: (]) (join (join (item (i) of [X List v]) [, ]) (join (item (i) of [Y List v]) [, ]))) (join (item (i) of [Z List v]) [)]))
change [i v] by (1)
wait (1) seconds
end
  

This will sort your points first by Y (lowest first), then by X within the same Y, then by Z within the same X and Y! 🎉

Pro tip: For very large datasets, consider using the quicksort version as it’s much faster than bubble sort!

CS

CodeSorter92

Replied 15 minutes later

@AlgorithmExpert This is absolutely perfect! 🙌 The comparison function approach makes so much sense now!

I implemented the bubble sort version first and it works flawlessly. The logic flow diagram really helped me understand the priority system. Thanks for including both bubble sort and quicksort - I’ll definitely upgrade to quicksort once my dataset grows larger.

You’ve saved me hours of debugging! 🎉

DP

DataProcessor_Pro

Replied 2 hours later

Excellent solution @AlgorithmExpert! 👏 For anyone interested in optimizing further, here are some additional tips:

  • Memory optimization: If you’re dealing with thousands of points, consider using a single list with alternating X,Y,Z values instead of three separate lists
  • Stability: The bubble sort version is stable (maintains relative order of equal elements), while quicksort isn’t - choose based on your needs
  • Preprocessing: If you frequently sort the same type of data, consider caching comparison results

Multi-dimensional sorting is a fundamental skill in computational geometry - great question to tackle! 🚀

VB

Vibelf_Community

Pinned Message • Moderator

🧮 Master Advanced Algorithms with Expert Guidance

Fantastic discussion on multi-dimensional sorting! For those ready to dive deeper into advanced algorithms and data structures, our expert tutors can help you master:

  • 🔄 Advanced sorting algorithms (merge sort, heap sort, radix sort)
  • 🌳 Tree-based data structures and spatial indexing
  • 📊 Algorithm complexity analysis and optimization
  • 🎯 Real-world applications in game development and graphics

📚 Related Advanced Topics

Ready to become an algorithm expert? Get personalized guidance from our computer science tutors!