DEV Community

Peter + AI
Peter + AI

Posted on

πŸš€ Mastering the Uniface sort Statement: A Complete Guide

⚠️ This blog post was created with the assistance of AI to provide accurate and comprehensive information about Uniface development.

Sorting data is a fundamental operation in any database application. If you're working with Uniface 10.4, the sort statement is your go-to tool for organizing data efficiently. Let's dive deep into how this powerful command works! πŸ“Š

πŸ” What is Uniface?

Before we explore the sort statement, let's quickly understand what Uniface is. Uniface is a low-code development platform that helps developers create enterprise applications. It uses a special programming language called ProcScript - a fourth-generation language (4GL) that makes database operations much simpler than traditional programming languages.

Think of Uniface as a tool that sits between your application and the database, handling all the complex database communications for you. ✨

πŸ“‹ Understanding Key Terms

Let's clarify some important Uniface concepts:

  • Entity: Think of this as a database table. It represents a collection of related data (like PERSON, ORDER, or PRODUCT).
  • Occurrence: This is like a row in a database table. Each occurrence represents one record.
  • Hitlist: This is Uniface's way of storing query results in memory. It's like a temporary collection of records that match your search criteria.
  • Active Path: This defines the relationship chain that Uniface follows when processing data across multiple related entities.

🎯 The sort Statement Basics

The sort statement organizes the data in your hitlist according to specific criteria. Here's the basic syntax:

sort Entity, Field:SortOptions
Enter fullscreen mode Exit fullscreen mode

Where:

  • Entity: The name of the entity (table) you want to sort
  • Field: The field (column) to sort by
  • SortOptions: Additional options that control how the sorting works

πŸ› οΈ Sort Options Explained

Uniface gives you powerful control over how your data gets sorted:

πŸ“ˆ Order Options

  • a or ascending: Sort from A to Z, 1 to 9 (default)
  • d or descending: Sort from Z to A, 9 to 1

πŸ”„ Uniqueness Options

  • u or unique: Remove duplicate values, keeping only the first occurrence

🌐 Type Options

  • numeric: Treat data as numbers (so 2 comes before 10)
  • date: Sort as dates
  • ci: Case-insensitive sorting (A and a are treated the same)
  • cs: Case-sensitive sorting (A and a are treated differently)

πŸ’‘ Practical Examples

Example 1: Basic Sorting

retrieve/e "PERSON"
sort "PERSON", "LAST_NAME:a"
Enter fullscreen mode Exit fullscreen mode

This retrieves all person records and sorts them by last name in ascending order. πŸ‘₯

Example 2: Multi-Level Sorting

sort "PERSON", "LAST_NAME:a;FIRST_NAME:a"
Enter fullscreen mode Exit fullscreen mode

This sorts by last name first, then by first name for people with the same last name. Perfect for creating alphabetical lists! πŸ“

Example 3: Removing Duplicates

sort "ORDERS", "CUSTOMER_ID:u"
Enter fullscreen mode Exit fullscreen mode

This gives you a unique list of customer IDs, removing any duplicates. Great for finding all customers who have placed orders! πŸ›’

Example 4: Numeric Sorting

sort "PRODUCTS", "PRICE:d numeric"
Enter fullscreen mode Exit fullscreen mode

This sorts products by price from highest to lowest, treating the price field as numbers rather than text. πŸ’°

⚠️ Important Limitations and Considerations

  • Data Size Limit: Sorting works with the first 8KB of field data and maximum 32KB total per occurrence
  • Hitlist Completion: Using sort automatically completes your hitlist, which might affect performance with large datasets
  • Active Path: You can sort on fields from related entities as long as they're on the active path
  • Deprecated Feature: The sort/e syntax is deprecated - use sort instead

πŸ› Common Issues and Solutions

Problem: Numbers Sorting as Text

Issue: "10" appears before "2" when sorting

Solution: Add the numeric option:

sort "ITEMS", "ITEM_NUMBER:a numeric"
Enter fullscreen mode Exit fullscreen mode

Problem: Case-Sensitive Sorting Issues

Issue: "Apple" and "apple" aren't grouped together

Solution: Use case-insensitive sorting:

sort "PRODUCTS", "NAME:a ci"
Enter fullscreen mode Exit fullscreen mode

Problem: Entity Not Found Error

Issue: Getting error -1102 (UPROCERR_ENTITY)

Solution: Make sure the entity name is correct and the entity is properly painted on your component

βœ… Best Practices

  • Always retrieve before sorting: Make sure you have data in your hitlist first
  • Use appropriate data types: Specify numeric, date, or other types when needed
  • Consider performance: Large datasets might take time to sort - consider pagination
  • Handle errors: Always check $status and $procerror after sorting
  • Use meaningful field names: This makes your code more readable and maintainable

πŸŽ‰ Conclusion

The Uniface sort statement is a powerful tool that gives you fine-grained control over how your data is organized. Whether you're creating simple alphabetical lists or complex multi-level sorts with duplicate removal, this statement has you covered. 🎯

Remember to always test your sort operations with sample data and consider the performance implications when working with large datasets. With practice, you'll be sorting Uniface data like a pro! πŸš€

Happy coding! πŸ’»βœ¨

Top comments (0)